# ts-sum-types

> Algebraic sum types for TypeScript, designed after Rust's enums

Latest version **0.2.1** (published 2019-06-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install ts-sum-types
pnpm add ts-sum-types
yarn add ts-sum-types
bun add ts-sum-types
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.1 |
| Published | 2019-06-12 |
| First published | 2019-06-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 7.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | kinrany |
| Maintainers | kinrany |
| Keywords | typescript, sum-types, algebraic-data-types, tagged-unions, enums |

## Links

- npm: https://www.npmjs.com/package/ts-sum-types
- Repository: https://github.com/Kinrany/ts-sum-types
- Homepage: https://github.com/Kinrany/ts-sum-types#readme
- Issues: https://github.com/Kinrany/ts-sum-types/issues
- npm.io page: https://npm.io/package/ts-sum-types

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.2.1 (latest) — 2019-06-12
- 0.2.0 — 2019-06-12
- 0.1.1 — 2019-06-12
- 0.1.0 — 2019-06-12

## README

# ts-sum-types

## Overview
Algebraic sum types (aka [tagged unions]) for TypeScript.

Designed after [Rust's enums].

[tagged unions]: https://en.wikipedia.org/wiki/Tagged_union
[Rust's enums]: https://doc.rust-lang.org/stable/rust-by-example/custom_types/enum.html

## Installation

```bash
npm install --save ts-sum-types
```

This library exports a ES6 module with types.

```typescript
import { create_enum_namespace, type, EnumUnion} from 'ts-sum-types';

// Create an `enum` to classify a web event.
const WebEvent = create_enum_namespace({
  // An `enum` may either be `unit-like`,
  PageLoad: type(),
  PageUnload: type(),
  // like tuple structs,
  KeyPress: type<number>(),
  Paste: type<string>(),
  // or like structures.
  Click: type<{ x: number; y: number }>(),
});
type WebEvent = EnumUnion<typeof WebEvent>;

// A function which takes a `WebEvent` enum as an argument
function inspect(event: WebEvent) {
  event.match({
    PageLoad: () => console.log("page loaded"),
    PageUnload: () => console.log("page unloaded"),
    // Destructure `c` from inside the `enum`.
    KeyPress: (c) => console.log(`pressed '${c}'.`),
    Paste: (s) => console.log(`pasted "${s}".`),
    // Destructure `Click` into `x` and `y`.
    Click({ x, y }) {
      console.log(`clicked at x=${x}, y=${y}.`);
    },
  });
}
```

## Development

```bash
npm install
npm run build
npm publish
```

## Prior art

- [pelotom/unionize](https://github.com/pelotom/unionize)
- [twop/ts-union](https://github.com/twop/ts-union)

---
_Source: https://npm.io/package/ts-sum-types · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
