TypeScript for real code · Chapter 23 of 36
Types inside themselves
By the end of this chapter you can write a type that contains itself, walk it with a function that calls itself, and make a mapped type reach every level of an object. 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
Anjali wants the menu shown as a tree: Snacks inside the menu, Biscuits inside Snacks, and so on. Bunty typed a category's children as any[], so a bun maska with no price went live last night. Ravi wants to change one delivery fee without sending the whole settings object again. And the settings are saved as JSON, where a function or an undefined quietly disappears.
The types you know have a fixed depth. You can type a category with items, or a category with categories with items, but not "as deep as it goes". And Readonly<T> and Partial<T> from chapter 17 change only the top level of an object.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
JsonValue: any JSON, nested as deep as you like
A JSON value is a string, a number, a boolean, null, a list of JSON values, or an object with string keys whose values are JSON values. Replace any with a JsonValue type that says exactly that, using its own name for the list and the object.
Settings nested several levels deep (lists inside objects inside lists) are a JsonValue. undefined, an object with a field holding undefined, and a function, even deep inside a list, are type errors. saveSettings({ gst: 5, open: true }) is '{"gst":5,"open":true}'.
- Task 2
MenuNode: a tree of items and categories
A MenuNode is either an item, { kind: 'item'; name: string; price: number }, or a category, { kind: 'category'; name: string; children: MenuNode[] }, and a category can hold more categories. So an item without a price, a category without children, a kind like 'combo', or a name string in children is a type error, at any depth.
countItems(node) counts the items anywhere inside: an item is 1, an empty category is 0, and a category holding two items and a category with two more items inside is 4. Do not count the categories.
- Task 3 · interview question
Interview: DeepReadonly, nothing inside can change
DeepReadonly<T> makes every property readonly at every level: nested objects, lists, and objects inside lists. For a DeepReadonly shop, changing shop.name, shop.delivery.fee or shop.menu[0].price is a type error, and so is pushing to shop.delivery.zones. Reading still works: shop.delivery.fee is a number and shop.menu[0].price is a number. It must be a real type, not any.
Write it as a mapped type that uses itself. Readonly<T> alone locks only the top level. Dev will ask why Readonly is not enough and how your type reaches the lists.
- Task 4
applyPatch: a DeepPartial patch for the settings
DeepPartial<T> makes every property optional at every level, so { a: { b: { c: 1 } } } is a DeepPartial of { a: { b: { c: number; d: string } } }. It must be a real type, not any. A fee written as text, or a field Settings does not have (like a colour in delivery), is a type error.
applyPatch(current, patch) returns new Settings with the patch's fields changed and everything else kept: applyPatch(settings, { delivery: { fee: 30 } }) has delivery { fee: 30, freeAbove: 300 }, the same hours and the same shopName. A patch can change shopName and hours together, and {} changes nothing. It must not change current.
What you will learn
- Recursive types: a type that uses its own name, for trees and nested data.
- Recursive functions: a function that calls itself to walk such a type.
- Recursive mapped types: a mapped type that calls itself for every property that is an object, lists included.
More in Types that compute types
Build "Types inside themselves" 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.