All academies
All challenges

Discriminated Union Areas

mediumtype-guards100 XP

Problem

Model shapes as a discriminated union: a square has a kind of "square" and a side, a rectangle has "rectangle" with width and height, and a triangle has "triangle" with base and height. Write area(shape: Shape): number using a switch on shape.kind, and include a default branch that assigns the shape to a variable of type never so the compiler errors if a new shape is ever added but not handled.

Expected output

16
15
12

Hints

Stuck? Reveal hints one at a time.

Solution

Try solving it yourself first — revealing is always available.

Press Run in the editor — your console output is checked automatically.
No errors
Loading...

Console output appears here. Press Run or just type — the code compiles automatically.