TypeScript traps · Chapter 5 of 16

An optional property set to undefined

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Data from outside
  • 3 tasks
  • TypeScript
  • In the all labs plan

The first 2 chapters of TypeScript traps are free. This one comes with the all labs plan.

Why you need this

A form that reads its pickers naturally builds the second kind:

type Alerts = { sound: string; vibrate: boolean };

function readPickers(soundPicker: string | undefined, vibrateSwitch: boolean | undefined): Partial<Alerts> {
  return { sound: soundPicker, vibrate: vibrateSwitch };
}
console.log(readPickers(undefined, undefined));
{ sound: undefined, vibrate: undefined }

typecheck prints No type errors. And when that is spread over the defaults, TypeScript types the result as a full Alerts, sound: string, because it reads ? as "maybe missing", not "maybe undefined".

There is a compiler option that makes the type honest, exactOptionalPropertyTypes: with it, sound?: string means "missing or a string", and an explicit undefined is refused. typecheck --exactOptionalPropertyTypes form.ts (trimmed):

form.ts:4:3 - error TS2375: Type '{ sound: string | undefined; vibrate: boolean | undefined; }' is not assignable to type 'Partial<Alerts>' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.

Few projects turn it on, Tapri included, so the code has to handle both.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    repro.ts: send size undefined to the kitchen on purpose

    Write reproduce() in repro.ts. Make the order the new screen sends when the customer does not touch the size and sugar pickers: makeOrder('Masala chai', { size: undefined, sugarSpoons: undefined }), from order-options.ts, with both keys there and set to undefined. Resolve to the kitchenLine of that order.

    The check runs your reproduce() twice: beside the original order-options.ts it must resolve to '1 x Masala chai, size undefined, sugar undefined' (the line from the ticket), and beside a fixed order-options.ts it must resolve to '1 x Masala chai, size regular, sugar 2'. So the choices must really be sent as undefined. No real waiting: the choices are an object you write in, and reproduce() must finish within 2 seconds.

  2. Task 2

    order-options.ts: keep the defaults when a choice is undefined

    Fix makeOrder(item, choices) in order-options.ts. A choice that is missing, or there but undefined, keeps its default (size 'regular', sugarSpoons 2, cups 1). A choice that is given wins, including sugarSpoons 0 (no sugar), size 'large' and any number of cups. It gives a new order object { item, size, sugarSpoons, cups }, with every choice filled in and no key left undefined, and never changes DEFAULTS.

    Keep Options, Order, DEFAULTS and kitchenLine as they are. The check uses its own orders and also type-checks order-options.ts. Falling back with || is not a fix: no sugar, 0 spoons, must stay 0, not become 2.

  3. Task 3

    order-options.test.ts: a test that would have caught it

    Write a vitest test file, order-options.test.ts, that would have caught this bug before the new screen shipped. The check runs your file three times: beside the original order-options.ts, where at least one test must fail; beside the fixed order-options.ts, where every test must pass; and beside a wrong fix that falls back with || for every default, where at least one test must fail.

    So test both sides: choices sent as undefined keep the defaults, and an order with no sugar, sugarSpoons 0, keeps 0 spoons. No real waiting: each test must finish within 3 seconds.

What you will learn

  • repro.ts: send size undefined to the kitchen on purpose
  • order-options.ts: keep the defaults when a choice is undefined
  • order-options.test.ts: a test that would have caught it

More in Data from outside

Build "An optional property set to undefined" yourself

Tapri moved to TypeScript, and Kabir, its senior developer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of TypeScript traps are free. This one comes with the all labs plan.