JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 19
  • Score
    100M100P100Q54972F
  • License MIT

Delightful Sum Types in TypeScript

Package Exports

  • itsamatch

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (itsamatch) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

NPM Package Build Size

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 two functions 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 :

import { DataType, genConstructors, match } from 'itsamatch';

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

// generate default variant constructors for lists of numbers
const { Nil, Cons } = genConstructors<List<number>>()('Nil', 'Cons');

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

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

More examples are available in the /examples folder