3 ms·
The nice thing about them is that you have been using them all the time if you do dynamic typing. They are the most natural way to encode structural informatio
by dgb23 4y ago
The nice thing about them is that you have been using them all the time if you do dynamic typing.
They are the most natural way to encode structural information, especially across language/system boundaries.
- Clojure’s multimethods (multiple dispatch)
- Clojure spec’s “or”
- JSON schema’s “anyOf”
There’s likely more features and tools that are not strictly/statically discriminated/tagged unions, but in practice they help you to code that way.
In TS this is IMO the best feature. In a sense it just adds tooling support on top of a coding style that is very close to dynamic typing.
- JoeyJoJoJr 4y agoIt is the way I always wanted to use JavaScript, but was too cumbersome to utilise without a good type system. My favourite use-case for discriminated unions is to represent all possible client events as a DU, as well as the entirety of the application state. This allows you to efficiently communicate with a backend over websockets via a very thin transport layer that only needs to handle one message type.
- pjmlp 4y agoCLOS multi-methods actually.