Hacker News new | ask | show | jobs
Show HN: Tagged Unions and Factories in TypeScript (gist.github.com)
2 points by nightlyherb 1206 days ago
Wish sum types got more love in TypeScript.

I have settled on this style to minimize the boilerplate code needed to write tagged unions and tagged union factories.

Lines 1-8 are helper functions for declaring tagged union types. They are used for non-generic tagged unions.

Non-generic tagged unions' types should be inferred correctly.

Generic tagged unions are super hard to type, as TypeScript only allows functions to be typed as generic values.

Wish you enjoy this as well.

1 comments

I really like the concept of tagged unions in TypeScript. I have difficulty understanding your code. Can you create a real life example?