As of version 2.0, Typescript supports tagged unions to some extent. The general syntax is
type MyType = A | B | C …;
Where A, B and C are interfaces. A MyType object can then be any of those types, and no other. The announcement gives a quick example:
interface Square {
kind: "square";
size: number;
}
interface Rectangle {
kind: "rectangle";
width: number;
height: number;
}
interface Circle {
kind: "circle";
radius: number;
}
type Shape = Square | Rectangle | Circle;
function area(s: Shape) {
// In the following switch statement, the type of s is narrowed in each case clause
// according to the value of the discriminant property, thus allowing the other properties
// of that variant to be accessed without a type assertion.
switch (s.kind) {
case "square": return s.size * s.size;
case "rectangle": return s.width * s.height;
case "circle": return Math.PI * s.radius * s.radius;
}
}
However, the type safety if these union types needs to be guarded and checked manually with a so-called “discriminant property type guard” as the example shows (checking s.kind).
taggued union. How can I use that in typescript, in a type-safe way ?