Delightful Sum Types in TypeScript
npm install itsamatch[![NPM Package][npm]][npm-url]
[![Build Size][build-size]][build-size-url]
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.
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 :
``typescript
import { DataType, constructors, match } from 'itsamatch';
// a list is a data type with two variants:
type List
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 =
Nil: () => 0,
Cons: ({ tail }) => 1 + len(tail)
});
const same =
'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