TypeScript traps · Chapter 6 of 16
Excess property checks that never ran
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
Put the same literal in a variable first, and pass the variable:
const changes = { zone: 'hsr layout', phnoe: '98450 22222' };
console.log(updateRider(imran, changes));
{ name: 'Imran', phone: '98450 11111', zone: 'hsr layout' }
typecheck prints No type errors. TypeScript's types are structural: an object with extra properties is still a RiderPatch if the ones it shares fit, because elsewhere in your code that same object may be used for more. The phone change is silently dropped.
There is one more check, for a type whose properties are all optional (a "weak" type): a value that shares none of them is refused, even from a variable. typecheck on weak.ts, which declares RiderPatch and a check(patch: RiderPatch) above these two lines:
const typoOnly = { phnoe: '98450 22222' };
check(typoOnly);
weak.ts:6:7 - error TS2559: Type '{ phnoe: string; }' has no properties in common with type 'RiderPatch'.
One correct key next to the typo, as in changes, is enough to pass it. (satisfies RiderPatch on the literal keeps it checked, for code you write; it cannot help with data you are handed.)
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: save a new price that never lands, on purpose
Write reproduce() in repro.ts. Take masala chai as it is on the menu, { id: 'masala-chai', name: 'Masala chai', price: 40, available: true }, and save Meera's change with saveFromForm from menu-admin.ts, using the form as the admin screen sends it: { name: 'Masala chai', price: '45' } (the price is text). Resolve to the price of the item saveFromForm gives back.
The check runs your reproduce() twice: beside the original menu-admin.ts it must resolve to 40 (the price stayed, as in the ticket), and beside a fixed menu-admin.ts it must resolve to 45. So it must really save the form with the price '45'. No real waiting: the item and the form are objects you write in, and reproduce() must finish within 2 seconds.
- Task 2
menu-admin.ts: refuse a field the menu does not have
Fix menu-admin.ts in two places. saveFromForm(item, form) sets the item's name and price from the form, the price turned from text into a number ('45' is 45). applyPatch(item, patch) throws an Error whose message is exactly unknown field: <key> for any key of the patch that is not name, price or available; id is not patchable either, so a patch with an id throws unknown field: id. When it throws, nothing is saved and the log line is not printed. A good patch gives a new item with those fields changed (available: false included) and leaves the item it was given as it was.
Keep MenuItem, MenuPatch, PriceForm and the log line menu: saved <id> as they are. The check passes its patches through variables, as the admin screen does, so the compiler cannot see their keys, and also type-checks menu-admin.ts. Fixing the typo alone is not a fix (the next typo is lost the same way), and neither is allowing any key the item has (id would be patchable).
- Task 3
menu-admin.test.ts: a test that would have caught it
Write a vitest test file, menu-admin.test.ts, that would have caught this bug before Meera's change went missing. The check runs your file four times: beside the original menu-admin.ts, where at least one test must fail; beside the fixed menu-admin.ts, where every test must pass; beside a wrong fix that only fixes the typo, and beside a wrong fix that allows any key the item has, where at least one test must fail each time.
So test three things: saving the form changes the price, a patch with a key that is not a menu field is refused, and a patch that changes id is refused. Build those patches in a variable first, as the admin screen does, so the file still type-checks. No real waiting: each test must finish within 3 seconds.
What you will learn
- repro.ts: save a new price that never lands, on purpose
- menu-admin.ts: refuse a field the menu does not have
- menu-admin.test.ts: a test that would have caught it
More in Data from outside
Build "Excess property checks that never ran" 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.