Command Palette

Search for a command to run...

Hectal
Phase 1: Basic Types and Annotations
TypeScript · Exercise 5 of 6 PreviousNext
Beginner

Use unknown, Not any

safeLength should accept a value of any kind and return its length if it is text, or null otherwise. Type the parameter as unknown (not any), give the function a return type, and narrow before using .length.

The checks also read the source: the word any must not appear.

What Check tests

  1. Your code has no type errors
  2. The text "hello" has length 5
  3. A number gives the fallback -1
  4. The word any is not used
  5. The result can be null, so it can't be stored as a plain number

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.