4 ms·
TypeScript does support tagged unions, actually: https://www.typescriptlang.org/docs/handbook/advanced-types.html#discriminated-unions https://www.typescriptlan
by bcherny 7y ago
TypeScript does support tagged unions, actually: https://www.typescriptlang.org/docs/handbook/advanced-types.html#discriminated-unions https://www.typescriptlang.org/docs/handbook/advanced-types....
As does Flow: https://flow.org/blog/2015/07/03/Disjoint-Unions/ https://flow.org/blog/2015/07/03/Disjoint-Unions/
- panzerklein 7y agoEvery language can do tagged unions with "kind" or "tag" markers and switch statement. But it's going to be verbose without pattern matching/destructuring combo.
- bcherny 7y agoIt’s not that bad, to be honest. Maybe a few more keystrokes than Haskell, but so is everything else in a C-style language. Definitely a fair point that it is a few more keystrokes, though.
- frankpf 7y agoThankfully TypeScript's type system is powerful enough (and JS's syntax flexible enough) to decently simulate pattern matching: https://gist.github.com/frankpf/cde7f792580f731dfe886ed2d91bab45 https://gist.github.com/frankpf/cde7f792580f731dfe886ed2d91b... (see the second file for usage)