TypeScript traps · Chapter 10 of 16
A clone that lost its class
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
typecheck on that file prints No type errors., because of how structuredClone is declared in Node's types (trimmed):
function structuredClone<T>(value: T, options?: { transfer?: Transferable[] }): T;
It promises to give back the same type it was given. For data that is true; for a class instance it is not, and the compiler takes the promise at its word. A spread is typed more honestly: { ...new Trip() } has the type { stops: string[] }, and calling addStop on it is refused:
spreadtype.ts:10:8 - error TS2339: Property 'addStop' does not exist on type '{ stops: string[]; }'.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: press Undo and break the receipt
Write reproduce() in repro.ts. Make a new Cart and a new CartHistory from cart.ts, then do what the customer did: add masala chai at 40 rupees, save the cart, add a samosa at 20 rupees, and press Undo with undo(). Resolve to receipt() of the cart undo() gave back.
The check runs your reproduce() twice: beside the original cart.ts it must resolve to 'Total: ₹undefined' (the receipt from the ticket), and beside a fixed cart.ts it must resolve to 'Total: ₹40'. So it must really save the cart with the masala chai in it, and undo. No real waiting: reproduce() must finish within 2 seconds.
- Task 2
cart.ts: make Undo give back a real cart
Fix CartHistory in cart.ts so that undo() gives back a real Cart: an instance of the class (instanceof Cart), so add() works on it and its total is right, holding the lines the cart had when it was saved. The copy must be deep: changing the live cart after save(), adding a new item or more of one it already has, must not change the saved copy, and changing the cart undo() gave back must not change the live one. undo() gives the copies back newest first, and throws an Error when there is nothing to undo.
Keep Cart's lines, add() and total, receipt(), and the log line undo: <n> line(s) back. The check uses its own carts and type-checks cart.ts. A copy that is a Cart again but shares the lines, or the line objects, with the live cart is not a fix: Undo would not undo.
- Task 3
cart.test.ts: a test that would have caught it
Write a vitest test file, cart.test.ts, that would have caught this bug before Undo shipped. The check runs your file three times: beside the original cart.ts, where at least one test must fail; beside the fixed cart.ts, where every test must pass; and beside a wrong fix whose copy is a Cart again but shares the live cart's lines, where at least one test must fail.
So save a cart, change the live cart after saving (add another item, or more of one), undo, and expect the old total from what you get back; then add to it and check its total again. No real waiting: each test must finish within 3 seconds.
What you will learn
- repro.ts: press Undo and break the receipt
- cart.ts: make Undo give back a real cart
- cart.test.ts: a test that would have caught it
More in Objects
Build "A clone that lost its class" 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.