# @d-exclaimation/union

> Simple union and pattern matching for TypeScript

Latest version **0.2.0** (published 2022-12-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install @d-exclaimation/union
pnpm add @d-exclaimation/union
yarn add @d-exclaimation/union
bun add @d-exclaimation/union
```

## Health

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

Positive: has types; no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2022-12-19 |
| First published | 2022-12-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 13.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | d-exclaimation |
| Maintainers | dexclaimation |
| Keywords | TypeScript, Functional, pipe, Functional programming |

## Links

- npm: https://www.npmjs.com/package/@d-exclaimation/union
- Repository: https://github.com/d-exclaimation/union
- Homepage: https://github.com/d-exclaimation/union#readme
- Issues: https://github.com/d-exclaimation/union/issues
- npm.io page: https://npm.io/package/@d-exclaimation/union

## 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.0 (latest) — 2022-12-19
- 0.1.1 — 2022-12-05
- 0.1.0 — 2022-12-05

## README

# Union

Simple union and pattern matching for TypeScript

## Setup

### Install
```sh
npm i @d-exclaimation/union
```

### Import
```typescript
import { Union, match } from "@d-exclaimation/union";
```

## Guide

Instead of declaring union like so

```ts
interface Cat {
  breeds: "Abyssinian" | "Shorthair" | "Curl" | "Bengal"
}

interface Dog {
  breeds: "Hound" | "Brittany" | "Bulldog" | "Boxer"
  color: "brown" | "white" | "black"
}


type Animal = Cat | Dog;
```

You can just do

```ts
import type { Union } from "@d-excclaimation/union";

interface Cat {
  breeds: "Abyssinian" | "Shorthair" | "Curl" | "Bengal"
}

interface Dog {
  breeds: "Hound" | "Brittany" | "Bulldog" | "Boxer"
  color: "brown" | "white" | "black"
}

//    v { __type: "cat" } & Cat | { __type: "dog" } & Dog
type Animal = Union<{
  "cat": Cat,
  "dot": Dog
}>;
```

### Narrow down union type

You can narrow back to the concrete type like so

```ts
import type { Union, Narrow } from "@d-excclaimation/union";

type Result = Union<{
  "ok": { data: any, info: string },
  "err": { message: string, trace: string[] }
}>;

type Ok = Narrow<Result, "ok">
//   ^ { __type: "ok", data: any, info: string }
```

### Pattern matching against union

Union can be pattern match with the `match` function

```ts
import { Union, match } from "@d-excclaimation/union";

type WebSocketMessage = Union<{
  "text": { message: string },
  "binary": { length: number }
}>;

match<WebSocketMessage, void>({ __type: "text", message: "Hello!" }, {
  text: ({ message }) => console.log(message),
  binary: ({ length }) => console.log(`Got binary with length of ${length}`),
});

/// prints: Hello!

match<WebSocketMessage, void>({ __type: "binary", length: 10 }, {
  text: ({ message }) => console.log(message),
  binary: ({ length }) => console.log(`Got binary with length of ${length}`),
});

/// prints: Got binary with length of 10
```

You can also provide default case using `"*"`

```ts
match<WebSocketMessage | { __type: "unknown" }, void>({ __type: "binary", length: 10 }, {
  text: ({ message }) => console.log(message),
  binary: ({ length }) => console.log(`Got binary with length of ${length}`),
  "*": () => console.log("Got an unexpected message")
});

/// prints: Got an unexpected message
```

## Feedback
If you have any feedback, feel free reach out at twitter [@d_exclaimation](https://www.twitter.com/d_exclaimation) or email at [thisoneis4business@gmail.com](thisoneis4business@gmail.com).

---
_Source: https://npm.io/package/@d-exclaimation/union · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
