• Stars
    star
    316
  • Rank 132,587 (Top 3 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Generate Algebraic Data Types and pattern matchers

ADTs for typescript

Enables succinct ADT declaration.

In typescript, Algebraic Data Types are encoded using union types with a discriminator field.

Example:

import { ADT } from "ts-adt";

type Option<A> = ADT<{
  some: { value: A };
  none: {};
}>;

This translates to:

type Option<A> = { _type: "some"; value: A } | { _type: "none" };

Here, Option<A> represents a value that can be one of two types; either it's an object with a _type attribute "some" and a value A, or it's an object with _type attribute "none". This type is quite useful, especially when used with Typescript's type narrowing feature:

declare const userImage: Option<string>;

function getUserImage(): string {
  if (userImage._type === "some") {
    return userImage.value; // value is accessible here, since _type is 'some'
  } else {
    return "http://example.com/defaultImage";
  }
}

Pattern matching

Pattern matching is a common usecase when dealing with ADTs. You'll often find yourself in a position where you need to default to certain values depending on what the underlying value is. For this reason, we've included a match and matchI function:

import { ADT, match, matchI } from "ts-adt";

declare const userImage: Option<string>;

const img = matchI(userImage)({
  some: ({ value }) => value,
  none: () => "http://example.com/defaultImage",
});

const img = pipe(
  userImage,
  match({
    some: ({ value }) => value,
    none: () => "http://example.com/defaultImage",
  })
);

Both functions work the same way, but their arguments are ordered differently for better inference in different cases.

Partial Matching

You can also use partial matching if you don't need to match against all possible cases.

Consider if you have an ADT of the shape:

type Xor<A, B> = ADT<{
  nothing: {};
  left: { value: A };
  right: { value: A };
}>;

declare const myXor: Xor<number>;

You can supply an "otherwise" function which gets invoked if the value doesn't match any of the supplied matchers. The otherwise function will be passed a value whose type is the types which weren't matched.

declare const myXor: Xor<number>;

matchPI(promise)({ nothing: () => 0 }, (rest) => {
  // rest inferred as {_type: 'left', value: number } | {_type: 'right', value: number }
  return rest.value;
});

Custom Discriminant field

By default, ADT and the match functions use the _type field as the discriminant. You can build an ADT using a different discriminant field, along with matching match functions by using the utilities in ts-adt/MakeMatch:

import { MakeADT, makeMatchers } from "ts-adt/MakeADT";

type Option<A> = MakeADT<
  "typ",
  {
    some: { value: A };
    none: {};
  }
>; // { typ: 'some', value: A } | { typ: 'none' }

const [match, matchP, matchI, matchPI] = makeMatchers("typ");

You can also use makeMatchers to build matchers for ADTs in other libraries:

import * as O from "fp-ts/Option";

const [match, matchP, matchI, matchPI] = makeMatchers("_tag");

pipe(
  O.fromNullable("value"),
  match({
    None: () => 0,
    Some: (v) => v.value,
  })
);

More Repositories

1

chainable-components

A composable API for reusable React code.
TypeScript
85
star
2

babel-plugin-monadic-do

Monads, Do style
JavaScript
38
star
3

ecma-proposal-chainable-do-syntax

24
star
4

fp-ts-lessons

TypeScript
20
star
5

lti-debugger

View and debug LTI requests within the browser
TypeScript
9
star
6

molecule

TypeScript
9
star
7

fish-completion-sync

A fish plugin to help dynamically load fish completions from `$XDG_DATA_DIRS`.
Shell
9
star
8

sift

A server that will read & store events that conform to IMSGlobal's Caliper specification
JavaScript
8
star
9

derivate

TypeScript
8
star
10

lti-example-java

An example Lti app that takes advantage of basic-lti-util
Java
4
star
11

typescript-tco

A typescript transformer which implements tail-call optimizations for recursive functions
TypeScript
4
star
12

docker-sakai

A dockerfile for the open source lms, Sakai
Shell
3
star
13

lmsrest

an abstract RESTful API for LMS-related objects
Java
3
star
14

blasteroids

an asteroids-like html5 game running on websockets
JavaScript
3
star
15

fp-ts-v1-v2-converter

TypeScript
3
star
16

casa-nodejs

JavaScript
2
star
17

aoc-2020-hs

Haskell
2
star
18

gbt

Graph Build Tool
TypeScript
2
star
19

lti-java

A library for writing LTI applications on the JVM
Java
2
star
20

paulgray.net

JavaScript
2
star
21

bun-flake

A nix flake definition for bunjs
Nix
2
star
22

mock-lti2-consumer

A Mock implementation of the LTI2 Consumer specification for testing purposes
Java
1
star
23

redux-typed

Explorations into redux + type safety via typescript
TypeScript
1
star
24

provisioning

Custom environment configuration via nix
HTML
1
star
25

babel-plugin-for-yield-as-do-while

For-Yield in JS, via Do-While
JavaScript
1
star
26

fp-in-scala-in-typescript

Solutions for "Functional Programming in Scala" ported to Typescript
TypeScript
1
star
27

worktreez

TypeScript
1
star
28

yaltt

Yet Another LTI Test Tool
TypeScript
1
star