Command Palette

Search for a command to run...

Hectal
Phase 13: TypeScript in the Browser: DOM and Async
TypeScript · Exercise 5 of 6 PreviousNext
Intermediate

Accept only valid todos from a server

The helper fetchTodos() pretends to be a server: it returns a promise of unknown, a list where some items are wrong.

Write the type guard isTodo(v: unknown): v is Todo for { id: number; title: string; done: boolean }, then loadTodos(): Promise<Todo[]> that keeps only the valid items.

The program logs how many were valid. Do not use any.

What Check tests

  1. Your code has no type errors
  2. Two todos are valid
  3. The valid titles are Buy milk and Sleep
  4. isTodo narrows an unknown to Todo
  5. No any in your code

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.