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
- Your code has no type errors
- Two todos are valid
- The valid titles are Buy milk and Sleep
- isTodo narrows an unknown to Todo
- 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.
Press Run to see the page.
Console
Nothing printed yet.