TypeScript traps · Chapter 11 of 16
A union without an exhaustive check
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: settle a wallet payment into nowhere
Write reproduce() in repro.ts. Start an empty ledger (an empty list of entries) and settle one wallet payment into it with settle from payments.ts: { kind: 'wallet', paise: 4000, walletId: 'W-118' }. Resolve to the ledger, the entries settle added.
The check runs your reproduce() twice: beside the original payments.ts it must resolve to [] (the money went nowhere), and beside a fixed payments.ts it must resolve to [{ account: 'wallet', paise: 4000 }]. So it must really settle a wallet payment of 4000 paise. No real waiting: reproduce() must finish within 2 seconds.
- Task 2
payments.ts: settle every kind of payment, and make tsc say so
Fix settle(payment, ledger) in payments.ts so that every kind of payment adds one entry to the ledger, in its own account: upi to 'upi', card to 'card', cash to 'cash', wallet to 'wallet', with the payment's paise. A payment whose kind the union does not know (an old app can still send one) must make settle throw an Error and add nothing to the ledger, and so must settleDay.
Then write and export assertNever(value: never): never, which throws an Error, and call it where no case matched, so the next kind that joins the union stops compiling until the switch has a case for it. Keep Payment, Account, Entry, settleDay and its log line. The check uses its own payments and a kind the union does not know, and also type-checks payments.ts and assertNever's parameter. Settling unknown kinds into one of the accounts is not a fix.
- Task 3
payments.test.ts: a test that would have caught it
Write a vitest test file, payments.test.ts, that would have caught this bug before Tapri Wallet launched. The check runs your file three times: beside the original payments.ts, where at least one test must fail; beside the fixed payments.ts, where every test must pass; and beside a wrong fix that settles every kind it has no case for into the UPI account, where at least one test must fail.
So settle a wallet payment and expect it in the wallet account, not only somewhere: one entry, account 'wallet', the payment's paise. A test of every kind is even better. No real waiting: each test must finish within 3 seconds.
What you will learn
- repro.ts: settle a wallet payment into nowhere
- payments.ts: settle every kind of payment, and make tsc say so
- payments.test.ts: a test that would have caught it
More in Objects
Build "A union without an exhaustive check" 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.