4 ms·
You might find my blog post on some of the uses interesting: https://blog.waleedkhan.name/union-vs-sum-types/ https://blog.waleedkhan.name/union-vs-sum-types/
by arxanas 5y ago
You might find my blog post on some of the uses interesting: https://blog.waleedkhan.name/union-vs-sum-types/ https://blog.waleedkhan.name/union-vs-sum-types/
In a language like TypeScript, they can be used for function overloading since there is no runtime dispatch based on argument type. But usually they're used as sum types in the same way as e.g. Rust enums.
Typescript supports so-called "singleton types", in which a given type is inhabited by exactly one value. This lets you do things like specify not just `string` but specific values like `"foo" | "bar"` as a type. In these cases, I often use them in combination with Typescript's "mapped types", which let you index into a map with a certain type and get another type out. This can be useful for things like RPC calls, where the type of the output depends on the type of the input.
- jb_s 5y ago>since there is no runtime dispatch based on argument type Ah, this answers my question I guess. Thanks for the info. I'm about to start a new job doing Typescript stuff so I am going to be deep diving into the language to get my head around it, so this helps a lot :-)