TypeScript traps · Chapter 14 of 16

Narrowing that went stale across an await

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

  • Part: Objects
  • 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.

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: cancel during the SMS on purpose

    Write reproduce() in repro.ts. Build an order with a rider, { id: 4630, customerPhone: '+91 98450 40001', cancelled: false, rider: { name: 'Sunita', phone: '+91 98450 22002' } }, and a fake SMS gateway whose first send holds its answer back: it returns a promise you settle yourself, later; any later send can answer straight away. Start notifyPickup(order, sms) from pickup.ts without awaiting it, cancel the order with cancelOrder while the customer's SMS waits, then let that SMS answer, await the pickup and resolve to what it resolved to. If it rejects, catch the error and resolve to its message.

    The check runs your reproduce() twice: beside the original pickup.ts it must resolve to "Cannot read properties of undefined (reading 'phone')" (the error from the ticket), and beside a fixed pickup.ts it must resolve to 'cancelled'. So the cancel must really happen while the SMS waits. No real waiting and no timers: you decide when the SMS answers, and reproduce() must finish within 2 seconds.

  2. Task 2

    pickup.ts: look again after the await

    Fix notifyPickup(order, sms) in pickup.ts so it looks at the order again after the customer's SMS has gone, before it messages the rider. A normal pickup sends the customer's SMS, then, once it has answered, the rider's, and resolves to 'notified'. An order cancelled while the customer's SMS was going out gets no rider SMS and resolves to 'cancelled'. If the order's rider was changed while the customer's SMS was going out, the rider SMS goes to the order's rider at that moment, the new one, and never to the old one. An order already cancelled, or with no rider, gets no SMS at all, as today, and a failed SMS still makes notifyPickup reject with the gateway's error.

    Keep cancelOrder, the types, both SMS texts and the log line order <id>: pickup notified as they are. The check uses its own orders, holds every SMS answer back and changes the order while it waits, and also type-checks pickup.ts. Copying the rider into a variable before the await is not a fix: a cancelled order's rider must not get the SMS.

  3. Task 3

    pickup.test.ts: a test that would have caught it

    Write a vitest test file, pickup.test.ts, that would have caught this bug before the first cancel during an SMS. The check runs your file three times: beside the original pickup.ts, where at least one test must fail; beside the fixed pickup.ts, where every test must pass; and beside a wrong fix that copies the rider before the await, where at least one test must fail.

    So hold the customer's SMS back with a fake gateway, as in your repro, cancel the order while it waits, then let it answer: expect notifyPickup to resolve to 'cancelled' and expect that no SMS went to the rider. A normal pickup can be a second test, resolving to 'notified'. No real waiting: each test must finish within 3 seconds.

What you will learn

  • repro.ts: cancel during the SMS on purpose
  • pickup.ts: look again after the await
  • pickup.test.ts: a test that would have caught it

More in Objects

Build "Narrowing that went stale across an await" 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.