mediumExtra#93

TypeScript unions and narrowing

Prompt

Define a type Shape that is either Circle ({ kind: 'circle', radius: number }) or Square ({ kind: 'square', side: number }). Write a function area that narrows the type on the discriminant and returns the area. Handle unknown kinds explicitly — in TypeScript that is an exhaustiveness check with never; at runtime, throw.

Hints

Solution

Your Code

10 min

Tests

Click Run to test your code