Exhaustiveness Checking with the Assert never Technique
function area(shape: Shape) {
switch (shape.kind) {
case "circle": // shape is Circle
return Math.PI * shape.radius * shape.radius;
case "triangle": // shape is Triangle
return (shape.x * shape.y) / 2;
case "square": // shape is Square
return shape.x * shape.x;
case "rectangle":
return shape.x * shape.y;
default: // shape is never
assertNever(shape); // shape can be passed to assertNever!
}
}