TypeScript traps · Chapter 7 of 16
Two ids that look the same
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
TypeScript's types are structural: the compiler compares what a type is made of, not what it is called. TableNo and Guests are both just number, so every number fits both. The same goes for objects with different names and the same shape:
type Kulhad = { ml: number };
type Tumbler = { ml: number };
function fill(glass: Tumbler): string {
return `tumbler, ${glass.ml} ml`;
}
const kulhad: Kulhad = { ml: 90 };
console.log(fill(kulhad));
tumbler, 90 ml
Usually that is what you want. For ids it is not: a rider id and an order id are different things that happen to be stored the same way.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
repro.ts: book Sunita's delivery under the wrong rider
Write reproduce() in repro.ts. Start an empty delivery book (a new Map), complete one order with completeOrder from deliveries.ts: order 1042, masala chai, delivered by rider 7 (Sunita), with the ids made by orderId and riderId. Resolve to deliveriesOf(book, riderId(7)), the order ids booked under Sunita.
The check runs your reproduce() twice: beside the original deliveries.ts it must resolve to [] (Sunita's delivery is lost), and beside a fixed deliveries.ts it must resolve to [1042]. So it must really complete order 1042 for rider 7. No real waiting: reproduce() must finish within 2 seconds.
- Task 2
deliveries.ts: make a rider id and an order id different types
Fix completeOrder in deliveries.ts so that every order is booked under the rider who delivered it: deliveriesOf(book, rider) gives that rider's order ids, in the order they were completed, and nothing for an id that is not a rider's. Then brand both ids: RiderId and OrderId must each be a number with its own brand, so that a plain number, or an id of the other kind, is a type error where one of them is wanted. riderId(n) and orderId(n) keep their checks (a whole number above 0, or they throw) and give back the branded types; at runtime an id is still the plain number.
Keep the other exports, the Order and DeliveryBook types and the log line payout: rider <r> delivered order <o>. The check uses its own riders and orders, and also type-checks deliveries.ts: recordDelivery with a plain number for the rider or for the order, or with the two ids swapped, must not compile, and a rider id then an order id must.
- Task 3
deliveries.test.ts: a test that would have caught it
Write a vitest test file, deliveries.test.ts, that would have caught this bug before it reached the riders' payouts. The check runs your file twice: beside the original deliveries.ts, where at least one test must fail, and beside the fixed deliveries.ts, where every test must pass.
So complete orders for two riders whose ids are not the orders' ids, and expect each rider's own orders back from deliveriesOf. Make the ids with riderId and orderId. vitest does not check types, so the test checks what the code does. No real waiting: each test must finish within 3 seconds.
What you will learn
- repro.ts: book Sunita's delivery under the wrong rider
- deliveries.ts: make a rider id and an order id different types
- deliveries.test.ts: a test that would have caught it
More in Data from outside
Build "Two ids that look the same" 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.