Command Palette

Search for a command to run...

Hectal
Phase 4: Unions, Literals and Narrowing
TypeScript · Exercise 2 of 6 PreviousNext
Beginner

Narrow with typeof

describe takes a string | number | null. For text it returns the text in capital letters, for a number it returns the number with two decimals (toFixed(2)), and for null it returns "nothing".

The starter treats every value as text, which TypeScript rejects. Use typeof checks and early returns so each branch only uses what is valid for it.

What Check tests

  1. Your code has no type errors
  2. The console prints HELLO, 3.14 and nothing
  3. A boolean is still 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.