Make area(shape) exhaustive, so adding a shape without handling it is a compile error.
Someone added triangle to Shape, and area doesn't handle it. Handle it, and write an assertNever(value) helper for the default case: any call where value could still be something is a compile error, and at runtime it throws.
type Shape = | { kind: "circle"; radius: number } | { kind: "square"; size: number } | { kind: "triangle"; base: number; height: number }; function assertNever(value) { // Throw, and reject any call where value isn't never.} function area(shape: Shape): number { switch (shape.kind) { case "circle": return Math.PI * shape.radius ** 2; case "square": return shape.size ** 2; }}The answer
Write it yourself first. Then compare it with a reference solution and the checks it passes.
Checking your sign-in…