TypeScript for real code · Chapter 4 of 36
Shapes of things
By the end of this chapter you can write down the shape of an object, with read-only and optional properties and objects inside objects, and let TypeScript check every object against it. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Why 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
Anjali added cutting chai to the menu, and the app showed it at ₹undefined: she had typed prise: 25. A menu item's id got renamed by mistake, and three old orders lost their items. A rider's order crashed the tracking screen over a misspelled property, and Ravi wants to add the delivery fee to a bill, but the total only takes menu items.
JavaScript says nothing about any of that. So far you can type numbers, text and lists, but an object is still any (Bunty's favourite), and any property name, right or wrong, compiles. What is missing is the shape: which properties an object has, which may be missing, which must never change.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
MenuItem: a readonly id and an optional note
Make id readonly (nobody changes an id) and note optional (most items have none) in the MenuItem type. name stays a string, price stays a number that can still be changed, and an item with no price, or a note that is not text, is a type error.
Then fix describeItem so it shows the note only when there is one: describeItem({ id: 'm1', name: 'Masala chai', price: 40 }) is 'Masala chai ₹40', and with note: 'less sugar' it is 'Masala chai ₹40 (less sugar)'.
- Task 2
riderLabel: a real order type finds the typo
riderLabel says who is taking an order where: for { id: 'T-1', delivery: { rider: { name: 'Arjun' }, area: 'Indiranagar' } } it is 'Arjun to Indiranagar'.
Replace any with the order's real type: an id (a string) and a delivery holding rider, an object with a name (a string), and area (a string). You can write it inline or as a type of your own. So an order id on its own, an order with no rider, and a rider's name that is a number are all type errors. Bunty's code has a typo that any hides: once order has a real type, typecheck points at it.
- Task 3
exercise.ts: no type errors, and cutting chai costs 25
Anjali added cutting chai (₹25) to the menu, and typecheck has found her typo. Fix it, so cuttingChai.price is 25 and the whole file has no type errors.
This task is the whole file, so it is green only when tasks 1, 2 and 4 leave no errors either.
- Task 4 · interview question
Interview: totalOf, anything that has a price
totalOf adds up a bill. Ravi's app sends the delivery fee as { label: 'delivery', price: 20 }, which is not a menu item, so it does not fit. totalOf([masalaChai, fee]) is 60 and totalOf([]) is 0.
Change the parameter type so it asks only for what the function uses: a list of things with a price that is a number. It returns a number. A price written as text, like [{ price: '20' }], and a thing with no price, like [{ label: 'smile' }], are type errors. No any. Dev will ask why the fee did not fit before, when it has a price.
What you will learn
- Object types: the properties and their types, with readonly for ones that never change and ? for ones that may be missing.
- Nested types: a property can be an object type of its own.
- Excess property checks: a new object with a property the type does not have is an error, which catches typos.
- Structural typing: TypeScript compares shapes, not names, so a function can ask only for what it uses.
More in Why types
Build "Shapes of things" 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.