HomeChallengesRepsProblemsLeaderboardTake a challenge

Trust nothing that arrives as JSON

JSON.parse returns any, and that any has spread through the file: parseMenuItem returns it, so a price of "5" sails through. Contain it with unknown and a type guard, and make every function say what it returns.

Intermediate8 min on the clock5 graded checks · 100 pointsTypes That Catch Bugs
Graded by running your code against real cases.

What you build

  • Treat the parsed value as unknown
  • Write isMenuItem(x: unknown): x is MenuItem
  • Return MenuItem | undefined from parseMenuItem
  • Give every exported function an explicit return type; logOrder returns void

Done means

No any escapes JSON.parse, the guard is a predicate, every bad input is rejected, and every function declares its return type.

How it is graded

Published in full, before you start — every point is one of these and there is nothing else. Each one runs your code; it is not a search for keywords.

  1. No any, including the one from JSON.parse+25TypeScript basics
  2. isMenuItem is a type predicate on unknown+20TypeScript basics
  3. Each kind of bad input is rejected+25TypeScript basics
  4. Every exported function declares its return type+15TypeScript basics
  5. logOrder returns nothing+15TypeScript basics

What it teaches

The rest of Types That Catch Bugs