Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/nathsou/itsamatch

Delightful Sum Types in TypeScript
https://github.com/nathsou/itsamatch

Last synced: about 2 months ago
JSON representation

Delightful Sum Types in TypeScript

Awesome Lists containing this project

README

        

[![NPM Package][npm]][npm-url]
[![Build Size][build-size]][build-size-url]

# It's a match!

Itsamatch is a tiny set of types and utilities to define and use variants / tagged unions / sum types in a more declarative way in TypeScript.

## Usage

itsamatch exposes three functions (match, matchMany, genConstructors) and a few types that make it easier to construct data types.
Below is a simple example showing how it can be used to create a [linked list data type](https://en.wikipedia.org/wiki/Cons#Lists) :

```typescript
import { DataType, constructors, match } from 'itsamatch';

// a list is a data type with two variants:
type List = DataType<{
Nil: {},
Cons: { head: T, tail: List }
}>;

// generate default variant constructors for lists of numbers
const { Nil, Cons } = constructors>().get('Nil', 'Cons');

// use the match function to compute the length of a list
const len = (list: List): number => match(list, {
Nil: () => 0,
Cons: ({ tail }) => 1 + len(tail)
});

const same = (a: List, b: List): boolean => matchMany([a, b], {
'Nil Nil': () => true,
'Cons Cons': (l, r) => l.head === r.head && same(l.tail, r.tail),
_: () => false,
});

const size = len(Cons({ head: 1, tail: Cons({ head: 2, tail: Nil() }) })); // 2
const sameElems = same(Cons({ head: 1, tail: Nil() }), Nil()); // false

```

More examples are available in the /examples folder

[npm]: https://img.shields.io/npm/v/itsamatch
[npm-url]: https://www.npmjs.com/package/itsamatch
[build-size]: https://badgen.net/bundlephobia/minzip/itsamatch
[build-size-url]: https://bundlephobia.com/result?p=itsamatch