type Shape =
| { kind: "circle"; radius: number }
| { kind: "rect"; width: number; height: number }
| { kind: "triangle"; base: number; height: number };
function assertNever(value: never): never {
throw new Error("Unhandled kind: " + JSON.stringify(value));
}
function area(s: Shape): number {
switch (s.kind) {
case "circle":
return Math.PI * s.radius * s.radius;
case "rect":
return s.width * s.height;
// triangle is missing - right now it falls through to assertNever and throws
default:
return assertNever(s as never);
}
}
const triArea = area({ kind: "triangle", base: 6, height: 4 });