@rachel-barrett/variant-ts
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

variant-ts

This is helper library for constructing arbitrary variant types (aka sum types / discriminated unions) in typescript.

This library is intended to be used alongside fp-ts, and indeed this code would make sense as part of that library.

Example usage

import * as variant from "@rachel-barrett/variant-ts"
import { pipe } from "fp-ts/function"

type Media =
  | variant.Tagged<"book", number>
  | variant.Tagged<"film", string>
  | variant.Tagged<"song", string>

const Media = variant.module<Media>({
  book: (value: number) => variant.tagged(_book, value),
  film: (value: string) => variant.tagged(_film, value),
  song: (value: string) => variant.tagged(_song, value),
})

const _book = "book"
const _film = "film"
const _song = "song"

const exampleBook = Media.book(123)
const exampleFilm = Media.film("Harry Potter")

const isBook: (media: Media) => boolean = (media) =>
  pipe(
    media,
    variant.caseOfWithDefault(false)({
      [_book]: () => true,
    })
  )

Dependencies (0)

    Dev Dependencies (5)

    Package Sidebar

    Install

    npm i @rachel-barrett/variant-ts

    Weekly Downloads

    1

    Version

    1.0.2

    License

    ISC

    Unpacked Size

    29.7 kB

    Total Files

    21

    Last publish

    Collaborators

    • rachel-barrett