Discriminated Unions (or tagged union types) are a form of type guards where the type is narrowed based on a shared property.
In the following example, the type of
Person have a shared property called
type. Depending on the value of this property, TypeScript can narrow down the type from
Dog | Person to either
Because this allows the type to be discriminated, this technique is referred to as discriminating unions. This concept also exists in F# (Discriminated Unions in F#).