TypeScript for real code · Chapter 17 of 36
The toolbox
By the end of this chapter you can build a new type from one you already have: fewer fields, optional fields, fewer members of a union, or the types inside a function. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Generics
- 5 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
Ravi added a note field to orders, so riders could see "ring the bell twice". Three screens broke and two others ignored it, because the fields of Order are written out by hand in five places: the kitchen summary, the new-order form, the patch for changing an order, and two more. Each copy is one more place to forget.
With what you know so far, every one of those types is a fresh list of fields. Copy them and the copies drift apart. Bunty's way out was any for the patch, so a status of 'lost' or a total of '90' sails through. What Tapri needs is to write Order once and say "Order, but only these fields" or "Order, all optional".
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
updateOrder: change any field but the id
updateOrder(order, changes) returns a new order with the changes applied, and leaves the old one as it was: updateOrder(ORDERS[0], { status: 'out-for-delivery' }) has status 'out-for-delivery' and id 'o1', and ORDERS[0] is still 'brewing'.
Give changes a type built from Order instead of any: any of Order's fields except id, each one optional. So { status: 'brewing' }, { note: 'less sugar', total: 90 } and {} are fine, while { id: 'o9' }, a status of 'lost' and a total of '90' are type errors.
- Task 2
summarise: only id, status and total
The kitchen screen shows only id, status and total. Make OrderSummary exactly those three fields of Order, picked from Order, so its status is still an OrderStatus.
summarise returns an OrderSummary that holds only those three fields: summarise(ORDERS[1]) is { id: 'o2', status: 'placed', total: 25 }, with no customer and no items.
- Task 3
NewOrder: an order before Tapri fills it in
A new order from the app has every field of Order except id, status and total, which placeOrder fills in. Make NewOrder that type, built from Order instead of any: its keys are exactly customer, items and note, its items are an OrderItem[], and note stays optional.
placeOrder({ customer: 'Meera', items: [] }) is fine, with or without a note. A new order with an id or a status is a type error, and so is one without a customer.
- Task 4
FetchedMenu and ReceiptArgs: types from the functions
Name two types without writing them out, by reading them from the functions above them, so they follow any change to those functions.
FetchedMenu is what fetchMenu's promise gives once awaited: exactly MenuItem[]. ReceiptArgs is printReceipt's parameters as a tuple, exactly [order: Order, copies: number, language: 'en' | 'kn'], names included.
- Task 5 · interview question
Interview: countActive, no status forgotten
ActiveStatus is every OrderStatus except 'delivered' and 'cancelled', built from OrderStatus: exactly 'placed' | 'brewing' | 'out-for-delivery'.
countActive counts the orders in each active status, and returns every active status as a key, even with a count of 0: countActive(ORDERS) is { placed: 2, brewing: 1, 'out-for-delivery': 0 }, and countActive([]) is all three at 0. Give it the return type Record<ActiveStatus, number>, so its keys are exactly the active statuses and reading counts.delivered is a type error. Dev will ask how this type makes a forgotten status an error.
What you will learn
- Pick, Omit, Partial, Required, Readonly: a new object type from an old one, with fewer fields or different modifiers.
- Exclude and Extract: a smaller union from a bigger one.
- ReturnType, Parameters, Awaited: the types already written inside a function's signature, read back out.
More in Generics
Build The toolbox 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.