TypeScript traps · Chapter 4 of 16
Unchecked index access
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.
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: put Total: ₹NaN on a bill on purpose
Write reproduce() in repro.ts. Build the bill from the ticket, two masala chai and one kulhad chai: [{ item: 'masala chai', qty: 2 }, { item: 'kulhad chai', qty: 1 }]. Resolve to billText of that bill, from bill.ts, or, when billText throws, to the error's message.
The check runs your reproduce() twice: beside the original bill.ts it must resolve to 'Total: ₹NaN' (the bill from the ticket), and beside a fixed bill.ts it must resolve to 'not on the price list: kulhad chai' (the fixed code refuses kulhad chai, and you catch it). So the bill must really have the kulhad chai line. No real waiting: the bill is a list you write in, and reproduce() must finish within 2 seconds.
- Task 2
bill.ts: refuse an item that is not on the price list
Fix billTotal(lines) in bill.ts so it looks every item up safely. An item on the price list is billed at its price times its qty. An item that is not on the list makes it throw an Error whose message is exactly not on the price list: <item> (for kulhad chai: not on the price list: kulhad chai). An item whose price is 0, a glass of water, is on the list: it is billed at 0, not refused. billText gives Total: ₹<total> as before.
Keep PRICES, BillLine and billText as they are. The check uses its own bills, free water and unknown items too, and also type-checks bill.ts with --noUncheckedIndexedAccess on, so a price looked up in PRICES and used as a number without a check is a type error: try typecheck --noUncheckedIndexedAccess bill.ts in the terminal. Checking whether the price is truthy is not a fix: it refuses the free water.
- Task 3
bill.test.ts: a test that would have caught it
Write a vitest test file, bill.test.ts, that would have caught this bug before kulhad chai went on the board. The check runs your file three times: beside the original bill.ts, where at least one test must fail; beside the fixed bill.ts, where every test must pass; and beside a wrong fix that refuses any price that is falsy, where at least one test must fail.
So test both sides: an item that is not on the price list is refused (with its message, if you like), and a bill with a free glass of water totals right. No real waiting: each test must finish within 3 seconds.
What you will learn
- repro.ts: put Total: ₹NaN on a bill on purpose
- bill.ts: refuse an item that is not on the price list
- bill.test.ts: a test that would have caught it
More in Data from outside
Build "Unchecked index access" 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.