3 ms·
The fundamental problem is that Typescript (and JavaScript) doesn't have tagged unions. In Rust or even C++ using std::variant this isn't a problem.
by IshKebab 7y ago
The fundamental problem is that Typescript (and JavaScript) doesn't have tagged unions. In Rust or even C++ using std::variant this isn't a problem.
- bcherny 7y agoTypeScript 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)