TypeScript traps · Chapter 16 of 16

filter with an async predicate

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.

Why you need this

In lib.es5.d.ts, the callback filter takes is declared to return unknown (trimmed):

filter(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): T[];

Anything is truthy or not, so any return type is accepted, a Promise<boolean> too. Compare an if, where TypeScript does know that a promise as a condition is a mistake. In a file ifpromise.ts, if (pantry.has('saffron')) is refused (trimmed):

ifpromise.ts:3:5 - error TS2801: This condition will always return true since this 'Promise<boolean>' is always defined.

Inside filter, the same mistake has no such check.

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: offer a busy rider on purpose

    Write reproduce() in repro.ts. Build three riders, all on shift, in this order: { id: 2, name: 'Sunita', onShift: true }, { id: 1, name: 'Imran', onShift: true } and { id: 3, name: 'Salma', onShift: true }, and a fake status service whose isFree(riderId) resolves to false for Imran (id 1, out on a delivery) and true for the others. Call freeRiders from riders.ts with them, await it, and resolve to the names of the riders it gives, in order.

    The check runs your reproduce() twice: beside the original riders.ts it must resolve to ['Sunita', 'Imran', 'Salma'] (Imran offered, as in the ticket), and beside a fixed riders.ts it must resolve to ['Sunita', 'Salma']. So Imran must really be busy in your fake. No real waiting: the fake answers straight away, and reproduce() must finish within 2 seconds.

  2. Task 2

    riders.ts: ask first, then filter

    Fix freeRiders(riders, status) in riders.ts so it gives only the riders who are on shift and free right now. A rider who is not on shift is never offered, and the status service is never asked about them. A rider on shift is offered only when status.isFree resolves to true for their id; the service is asked once per rider on shift. The riders keep the order they came in, and freeRiders resolves only once it has the answers it needs.

    Keep Rider, RiderStatus and the log line dispatch: <n> riders free as they are. The check uses its own riders and a status service whose answers it holds back and gives in a different order from the questions, and also type-checks riders.ts. Filtering by onShift alone is not a fix: a rider on shift may be out on a delivery.

  3. Task 3

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

    Write a vitest test file, riders.test.ts, that would have caught this bug before Friday's dispatch update. The check runs your file three times: beside the original riders.ts, where at least one test must fail; beside the fixed riders.ts, where every test must pass; and beside a wrong fix that filters by onShift alone, where at least one test must fail.

    So give freeRiders a rider who is on shift but busy, with a fake status service, and expect them not to be offered while the free riders are, in order. No real waiting: each test must finish within 3 seconds.

What you will learn

  • repro.ts: offer a busy rider on purpose
  • riders.ts: ask first, then filter
  • riders.test.ts: a test that would have caught it

More in Objects

Build "filter with an async predicate" 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.