TypeScript traps · Chapter 9 of 16
readonly that is only skin deep
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.
- Task 1
repro.ts: change the customers' menu with a preview
Write reproduce() in repro.ts. Preview 20% off MENU with previewDiscount from offers.ts, the way Anjali did, and then resolve to the price of the first item on MENU itself (masala chai), not on the preview.
The check runs your reproduce() twice: beside the original offers.ts it must resolve to 32 (the customers' price changed), and beside a fixed offers.ts it must resolve to 40. So it must really preview 20% off MENU and then read MENU. No real waiting: reproduce() must finish within 2 seconds.
- Task 2
offers.ts: preview a discount without changing the menu
Fix previewDiscount(menu, percent) in offers.ts so that it gives a new list of new items, each with its price cut by percent and rounded to the nearest whole rupee with Math.round (25 at 15% off is 21.25, so 21; 50 at 15% off is 42.5, so 43), names and order kept, and leaves the menu it was given, the list and every item, exactly as it was. Previewing a preview works the same way.
Then make MENU's type readonly all the way down: the check type-checks offers.ts, and changing an item's price on MENU, or pushing a new item onto MENU, must both be type errors. Keep MENU's items and prices, the MenuItem fields and the log line offers: previewing <percent>% off <n> items. The check uses its own menus too and looks at them, and at MENU, after every preview. Copying the list is not a fix: the items in it are still the menu's own objects.
- Task 3
offers.test.ts: a test that would have caught it
Write a vitest test file, offers.test.ts, that would have caught this bug before Happy Hour. The check runs your file three times: beside the original offers.ts, where at least one test must fail; beside the fixed offers.ts, where every test must pass; and beside a wrong fix that copies the list but changes the same item objects, where at least one test must fail.
So test both sides: the preview has the discounted prices, and after the preview MENU's own prices are what they were. Looking at the returned list alone is not enough. No real waiting: each test must finish within 3 seconds.
What you will learn
- repro.ts: change the customers' menu with a preview
- offers.ts: preview a discount without changing the menu
- offers.test.ts: a test that would have caught it
More in Objects
Build "readonly that is only skin deep" 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.