Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Boxed: bringing algebraic types to TypeScript

Boxed: bringing algebraic types to TypeScript

Avatar for Matthias Le Brun

Matthias Le Brun

March 27, 2024
Tweet

More Decks by Matthias Le Brun

Other Decks in Technology

Transcript

  1. Matthias Le Brun @bloodyowl lead engineering manager chief shitpost office

    the easiest way to provide banking features (accounts, payments, cards…) we're hiring! @
  2. type State = { data?: Data; error?: Error; }; type

    Result<Data> = | Ok<Data> | Error<Error>; error data ERROR NULL NULL DATA NULL NULL ERROR DATA
  3. type State = { data?: Data; error?: Error; }; type

    Result<Data> = | Ok<Data> | Error<Error>; state ERROR DATA
  4. type State = { data?: Data; error?: Error; }; type

    Result<Data> = | Ok<Data> | Error<Error>; 2 2 1 1 4 2 x = + =
  5. + x

  6. [1, 2, 3].map(x => x * 2) // [2, 4,

    6] [1, 2, 3].flatMap(x => [x, x * 2]) // [1, 2, 2, 4, 3, 6]
  7. map: <A, B>(T<A>, (a: A) => B) => T<B>: flatMap:

    <A, B>(T<A>, (a: A) => T<B>) => T<B>;
  8. export declare const chainFirstTaskK: <A, B>(f: (a: A) = >

    T.Task<B>) = > <E>(first: TaskEither<E, A>) = > TaskEither<E, A> ah just what I was looking for
  9. «let's try to make these types work with a good

    DX in a structurally typed language» — me, allegedly drunk
  10. const parsed = input != null ? parseInput(input) : undefined;

    const transformed = parsed != null ? transform(parsed) : undefined; const printed = transformed != null ? print(transformed) : undefined; const value = printed != null ? prettify(printed) : "fallback"; input .map(parseInput) .flatMap(transform) .map(print) .map(prettify) .getWithDefault("fallback");