TypeScript for real code · Chapter 24 of 36
Checked but exact
By the end of this chapter you can check a value against a type without losing its exact details, and keep a list or a call's arguments exact with as const and const type parameters. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Types that compute types
- 4 tasks
- TypeScript
- In the all labs plan
The first 2 chapters of TypeScript for real code are free. This one comes with the all labs plan.
Why you need this
The 4 pm offer went live, 10% off masala chai, and the app showed ₹NaN. Bunty had written the offer with as Offer, and it was missing a field. Ravi's price screen crashed too: the menu's type says its keys are any string, so a wrong item id compiled. And the list of order steps has a typo that nothing caught.
You know three ways to give a value a type, and each falls short. An annotation checks the value, then forgets its details: the keys become string. as const keeps every detail but checks nothing. And as barely checks at all: it is you telling the compiler to stop asking.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
MENU: checked, with the exact item ids
MENU's annotation checks every item, but it makes ItemId plain string, so priceOf('masala') compiles and crashes. Check MENU with satisfies Record<string, MenuItem> instead of the annotation, so ItemId is exactly 'masala-chai' | 'cutting-chai' | 'bun-maska' and priceOf('masala') is a type error.
Every entry must still be a MenuItem, and a price read from MENU must still be a number (so no as const). priceOf('masala-chai') is 40, priceOf('bun-maska') is 30, and the keys stay in the same order.
- Task 2
ORDER_FLOW: a fixed list of real statuses
ORDER_FLOW lists the steps of an order in order: 'placed', 'brewing', 'out-for-delivery', 'delivered'. Make it a fixed list with as const, so it has exactly four steps and push is a type error, and check every entry against Status with satisfies readonly Status[]. That finds Bunty's typo: fix it.
Then make FlowStep the union of ORDER_FLOW's entries, taken from ORDER_FLOW itself, so FlowStep is exactly 'placed' | 'brewing' | 'out-for-delivery' | 'delivered' and nextStep('cancelled') is a type error. nextStep('placed') is 'brewing', nextStep('brewing') is 'out-for-delivery' and nextStep('delivered') is undefined.
- Task 3
defineSizes: keep the sizes' names with a const type parameter
defineSizes(['small', 'large']) must remember the exact sizes without the caller writing as const: the type of its sizes' entries is 'small' | 'large', and CUP's is 'small' | 'regular' | 'large'. Give it a const type parameter T that extends readonly string[], and take sizes as a T.
isSize(value) takes any string and is a type predicate: once it says yes, the string is one of the sizes ('small' | 'large' for that call). CUP.isSize('large') is true, CUP.isSize('huge') is false, and defineSizes(['kulhad']).isSize('kulhad') is true.
- Task 4 · interview question
Interview: rushHourOffer, satisfies instead of Bunty's as
The 4 pm offer is 10% off masala chai until 17:00, so offerPrice(rushHourOffer) must be 36, and rushHourOffer equals { item: 'masala-chai', percentOff: 10, until: '17:00' }. Bunty's as Offer silenced a mistake, and the app shows NaN.
Replace the cast with satisfies Offer, and fix what TypeScript then shows you. The offer must be a complete Offer, and its item must stay exactly 'masala-chai', not any ItemId. Dev will ask why as hid the bug and satisfies found it.
What you will learn
- satisfies: checks a value against a type, and keeps the value's own, more exact type.
- as const satisfies: a fixed list whose every entry is checked.
- Const type parameters: a generic function that keeps its arguments as exact as as const would, without the caller writing it.
More in Types that compute types
Build "Checked but exact" yourself
Tapri’s codebase grows, and types keep it honest. 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 for real code are free. This one comes with the all labs plan.