TypeScript for real code · Chapter 18 of 36
Types from values
By the end of this chapter you can take a type from a value that already exists, list its keys, and read the type of one property out of another type. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Generics
- 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
Meera added egg puffs to the menu on Monday. On Tuesday the app still refused to sell one: the menu object had the new dish, but Bunty's hand-typed list of menu ids did not. His copy of the settings type has drifted too: it says the delivery radius is text, and it forgot the opening time.
So far, every type you used was written by hand next to the value it describes. That is two copies of the same fact, and one of them is always the one nobody updated. Writing any instead hides the drift until a customer finds it. Tapri needs the value to be the only copy, with the types read from it.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
MenuId: the keys of MENU
MenuId is one of MENU's keys, exactly 'masala-chai' | 'cutting-chai' | 'bun-maska' | 'egg-puff'. Derive it from MENU instead of string, and do not list the ids by hand, so adding a dish to MENU adds its id.
priceOf('masala-chai') is 40 and priceOf('egg-puff') is 30, and priceOf('latte') is a type error.
- Task 2
Settings: the type of DEFAULT_SETTINGS
Settings is the type of DEFAULT_SETTINGS. Bunty's copy says the radius is a string and forgot openAt. Take the type from the value itself, so it is exactly { gstPercent: number; currency: string; openAt: string; deliveryRadiusKm: number }.
withSettings({ openAt: '06:30' }) keeps the other defaults: { gstPercent: 5, currency: '₹', openAt: '06:30', deliveryRadiusKm: 3 }. A radius written as text, like '5', is a type error.
- Task 3
OrderStatus and OrderLine: read out of Order
Read two types out of Order instead of repeating them. OrderStatus is the type of Order's status, exactly 'placed' | 'brewing' | 'delivered' | 'cancelled'. OrderLine is the type of one element of Order's items, exactly { name: string; qty: number; price: number }.
lineTotal({ name: 'bun maska', qty: 2, price: 35 }) is 70. canCancel is true for 'placed' and 'brewing' and false for 'delivered'. canCancel('lost') is a type error, and so is a line without a price.
- Task 4 · interview question
Interview: getSetting, the exact type of each setting
getSetting(key) returns that setting from current: getSetting('gstPercent') is 5, getSetting('currency') is '₹' and getSetting('deliveryRadiusKm') is 5.
Make it generic instead of string and any: the key must be a key of Settings, so getSetting('colour') is a type error, and the return type is that setting's own type, so getSetting('gstPercent') has the type number and getSetting('currency') the type string. Dev will ask why one parameter typed with all the keys of Settings is not enough.
What you will learn
- typeof in a type: the type of a value that already exists.
- keyof typeof: the keys of that value, as a union of strings.
- Indexed access: Delivery['zone'], the type of one property, and [number] for one element of a list.
More in Generics
Build "Types from values" 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.