Command Palette

Search for a command to run...

Hectal
Phase 4: Unions, Literals and Narrowing
TypeScript · Exercise 5 of 6 PreviousNext
Intermediate

Write a Type Guard

isUser checks that unknown data is a User (an object with a text name and a number age), but its return type is plain boolean, so TypeScript doesn't narrow. Change the return type to a type predicate.

The checks call isUser on unknown data and use .name and .age inside the if.

What Check tests

  1. Your code has no type errors
  2. The console prints ASHA 31 and not a user twice
  3. isUser narrows an unknown value

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.