HomeChallengesRepsProblemsLeaderboardTake a challenge

Don’t retype the type

Three hand-copied versions of MenuItem have drifted: one lost spicy, two let a caller choose the id, and the update replaces the whole item with the patch. Derive them from MenuItem instead, and protect the menu.

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

What you build

  • addItem(item: Omit<MenuItem, "id">): MenuItem assigns the next id
  • updateItem(id, patch: Partial<Omit<MenuItem, "id">>) changes only the given fields
  • type PriceTag = Pick<MenuItem, "name" | "price">
  • Export the menu as ReadonlyArray<MenuItem>, and delete the three copies

Done means

The derived types refuse what they should, the menu cannot be changed from outside, and add and update behave.

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. Refuses addItem with an id+20TypeScript basics
  2. Refuses a patch that changes the id+20TypeScript basics
  3. The menu cannot be changed from outside+15TypeScript basics
  4. PriceTag is exactly name and price+15TypeScript basics
  5. Ids are assigned in sequence; a patch changes only its fields+30TypeScript basics

What it teaches

The rest of Types That Catch Bugs