0.4.1 • Published 2 years ago

@unsplash/sum-types v0.4.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@unsplash/sum-types

Safe, ergonomic, non-generic sum types in TypeScript.

Documentation: unsplash.github.io/sum-types

import * as Sum from "@unsplash/sum-types"

type Weather = Sum.Member<"Sun"> | Sum.Member<"Rain", number>
const Weather = Sum.create<Weather>()

const getRainfall = Weather.match({
  Rain: n => `${n}mm`,
  Sun: () => "none",
})

const todayWeather = Weather.mk.Rain(5)

getRainfall(todayWeather) // '5mm'

Installation

The library is available on the npm registry: @unsplash/sum-types

Note that due to usage of Proxy and Symbol this library only supports ES2015+.

The following bindings are also available:

Motivation

The library solves a number of problems we've experienced at Unsplash with alternative libraries in this space. Specifically:

  • Convenient member constructor functions are provided, unlike ts-adt.
  • The API is small, simple, and boilerplate-free, unlike tagged-ts.
  • Pattern matching is curried for use in pipeline application and function composition, unlike @practical-fp/union-types.
  • Types are not inlined in compiler output, improving readability and performance at scale, unlike unionize.

The compromise we've made to achieve this is to not support generic sum types.

0.4.1

2 years ago

0.4.0

2 years ago

0.3.2

2 years ago

0.3.1

3 years ago

0.3.0

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.1

4 years ago

0.1.0

4 years ago