Discriminated Unions in Practice
This lesson contains some facts about discriminated unions that may come in handy when using them in the wild.
We'll cover the following...
We'll cover the following...
Non-string discriminators
In the example from the previous lesson, we used a string literal property called type
as a discriminator. First, the discriminator doesn’t have to be called "type"
. Any property name will be fine.
Second, the discriminator doesn’t have to be a string. You can also use number literals, Boolean literals, or even enums!
The built-in IteratorResult
type in TypeScript (starting from version 3.6) uses the Boolean property done
as a discriminator.
type IteratorResult<T, TReturn = any> = IteratorYieldResult<T> |
...