TypeScript traps · Chapter 8 of 16
=== with a string | number union
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
=== is true only when both sides have the same type and the same value. A number and a piece of text are never ===, however alike they look:
type Table = { no: number; guests: number };
function findTable(tables: Table[], no: string | number) {
return tables.find((table) => table.no === no);
}
const tables: Table[] = [{ no: 7, guests: 4 }];
console.log(findTable(tables, 7));
console.log(findTable(tables, '7'));
{ no: 7, guests: 4 }
undefined
typecheck on that file prints No type errors. The compiler does know this rule. Compare a number with a string directly, in check.ts (trimmed):
const no: number = 7;
const fromForm: string = '7';
console.log(no === fromForm);
check.ts:3:13 - error TS2367: This comparison appears to be unintentional because the types 'number' and 'string' have no overlap.
It only refuses a comparison that can never be true by the types. number and string | number overlap (both can be a number), so it lets the comparison through, and at runtime the text side never matches. The union hid the bug.
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: open an order from its SMS link and lose it
Write reproduce() in repro.ts. Make a list of orders that holds { id: 1042, item: 'Masala chai', status: 'on the way' } (other orders may be in it too) and open order 1042 the way the SMS link does: openFromLink from track-order.ts with the list and the link's id, the text '1042'. Resolve to the text it gives.
The check runs your reproduce() twice: beside the original track-order.ts it must resolve to 'Order not found' (what the customers saw), and beside a fixed track-order.ts it must resolve to 'Order 1042: Masala chai, on the way'. So the list must really hold that order. No real waiting: reproduce() must finish within 2 seconds.
- Task 2
track-order.ts: turn the link's text into an order id, once
Write and export parseOrderId(text: string): number | undefined in track-order.ts. Text made only of the digits 0 to 9 gives that number ('1042' gives 1042); anything else gives undefined: '', 'abc', '1042.0', ' 1042', '1042 ', '-7', '+5', '1e3', '0x10'. Then make findOrder take a number only, and make openFromLink parse the link's text with parseOrderId before it looks: it gives the order's status line, or 'Order not found' with the log line track: no order <link id>.
Keep TrackedOrder, statusLine and that log line as they are. The check uses its own orders and links, and also type-checks track-order.ts: findOrder with text for the id must be a type error, parseOrderId must take a string and return number | undefined. Number() alone is not a fix (Number('') is 0), and neither is ==.
- Task 3
track-order.test.ts: a test that would have caught it
Write a vitest test file, track-order.test.ts, that would have caught this bug before the SMS links went out. The check runs your file three times: beside the original track-order.ts, where at least one test must fail; beside the fixed track-order.ts, where every test must pass; and beside a wrong fix that compares with == instead, where at least one test must fail.
So test both sides through openFromLink only: a link with an order's id opens that order, and a link that only looks like it, such as '1042.0' or ' 1042', gives 'Order not found'. No real waiting: each test must finish within 3 seconds.
What you will learn
- repro.ts: open an order from its SMS link and lose it
- track-order.ts: turn the link's text into an order id, once
- track-order.test.ts: a test that would have caught it
More in Data from outside
Build "=== with a string | number union" 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.