Skip to content

Instantly share code, notes, and snippets.

@gormonn
Created February 6, 2020 15:29
Show Gist options
  • Save gormonn/6072cdb6c8d698fb5da2cf871e717335 to your computer and use it in GitHub Desktop.
Save gormonn/6072cdb6c8d698fb5da2cf871e717335 to your computer and use it in GitHub Desktop.
TS typeGuards
https://www.typescriptlang.org/docs/handbook/advanced-types.html#discriminated-unions
Discriminated Unions #
You can combine singleton types, union types, type guards, and type aliases to build an advanced pattern called discriminated unions, also known as tagged unions or algebraic data types. Discriminated unions are useful in functional programming. Some languages automatically discriminate unions for you; TypeScript instead builds on JavaScript patterns as they exist today. There are three ingredients:
Types that have a common, singleton type property — the discriminant.
A type alias that takes the union of those types — the union.
Type guards on the common property.
interface Square {
kind: "square";
size: number;
}
interface Rectangle {
kind: "rectangle";
width: number;
height: number;
}
interface Circle {
kind: "circle";
radius: number;
}
First we declare the interfaces we will union. Each interface has a kind property with a different string literal type. The kind property is called the discriminant or tag. The other properties are specific to each interface. Notice that the interfaces are currently unrelated. Let’s put them into a union:
type Shape = Square | Rectangle | Circle;
Now let’s use the discriminated union:
function area(s: Shape) {
switch (s.kind) {
case "square": return s.size * s.size;
case "rectangle": return s.height * s.width;
case "circle": return Math.PI * s.radius ** 2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment