Command Palette

Search for a command to run...

Hectal
Phase 17: Interviews and Capstone
TypeScript · Exercise 5 of 6 PreviousNext
Intermediate

Review: Make Every Case Count

The Shape type has three kinds. area ends with assertNever(shape), so the compiler reports that one kind is missing a case. Add the missing case to area.

Then write perimeter(shape: Shape): number, also with an exhaustive switch that ends in assertNever(shape). A circle's perimeter is 2 * Math.PI * radius, a square's 4 * side, a rectangle's 2 * (width + height).

The program logs area and perimeter of a few shapes. Do not remove assertNever.

What Check tests

  1. Your code has no type errors
  2. The rectangle area is 12
  3. The rectangle perimeter is 14
  4. The square perimeter is 20
  5. Both functions end in assertNever
  6. An unknown shape is rejected

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

Runs in your browser. Your code is saved here.

Console

Nothing printed yet.