@unsplash/sum-types
    TypeScript icon, indicating that this package has built-in type declarations

    0.2.2 • Public • Published

    @unsplash/sum-types

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

    Documentation: unsplash.github.io/sum-types

    Example:

    import { Member, create } from "@unsplash/sum-types"
    
    type Weather = Member<"Sun"> | Member<"Rain", number>
    
    const {
      mk: { Sun, Rain },
      match,
    } = create<Weather>()
    
    const getRainfall = match({
      Rain: n => `${n}mm`,
      Sun: () => "none",
    })
    
    const todayWeather = 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, as in our testing we've found that they introduce unsafety into pattern matching and complicate the API. We deem this acceptable as, in our experience, in an ecosystem which already contains the likes of fp-ts and remote-data-ts, non-generic sum types are good enough for almost all domain types written in real-world application code.

    Install

    npm i @unsplash/sum-types

    DownloadsWeekly Downloads

    66

    Version

    0.2.2

    License

    MIT

    Unpacked Size

    16.9 kB

    Total Files

    13

    Last publish

    Collaborators

    • unsplash