TypeScript for real code · Chapter 35 of 36
Real functions, perfect types
By the end of this chapter you can give a helper function a signature that keeps the caller's exact types: the keys, the parameters, each step's result, and the cases that cannot happen. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The interview
- 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
utils.ts is the file every screen at Tapri imports: pipe, pick, debounce, groupBy. Bunty wrote them all, they all work, and they are all typed with any. So every screen that calls them has lost its types: pick(order, ['id', 'total']).status compiles, and so does a debounced save called with the quantity as text. Dev makes it his third round: "Fix the signatures. The bodies are fine."
An any that goes into a helper comes out everywhere it is used. A plain type like string or object is not enough either: the helper works for many types, and each call needs its own.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
pipe: each step's type flows into the next
pipe(value, f1, f2, ...) passes the value through the functions in order: pipe(40, addGst, rupees) is '₹42', pipe(2, (n: number) => n + 1) is 3, and four steps work too. Support one to four functions.
Its type must follow the value: pipe(40, addGst) is a number, pipe(40, addGst, rupees) is a string, and with four steps each function's parameter is what the one before returns, without writing it (pipe(' masala chai ', (s) => s.trim(), (s) => s.split(' '), (words) => words.length, (n) => n > 1) is a boolean). A function that cannot take the previous result is a type error, so pipe(40, rupees, addGst) and pipe('forty', addGst) are refused. No any in what callers see.
- Task 2 · interview question
Interview: pick returns exactly the keys asked for
pick(obj, keys) copies just those keys: pick(order, ['id', 'total']) is { id: 'A1', total: 80 }, and pick(order, []) is {}. Its type is exactly the keys asked for: for an Order, pick(order, ['id', 'total']) is { id: string; total: number }, so reading status from it is a type error, and asking for a key the object does not have, like 'tip', is a type error too.
Use the built-in Pick for the result type. Building the copy key by key is the one place TypeScript cannot follow, so the body may start it with one cast instead of any. Dev will ask why pick needs two type parameters.
- Task 3
debounce: keep the parameters it wraps
debounce(fn, ms) gives back a function with fn's parameters that waits until calls stop for ms milliseconds, then calls fn once with the last arguments: three quick saves, ('A1', 1), ('A1', 2) and then ('A1', 3), save only ['A1', 3]. The body already does this.
Its type must keep fn's parameters: for a debounced (orderId: string, qty: number) => void, save('A1', '2') and save('A1') are type errors, and the function it gives back returns void. ms is a number, so '300' is a type error. Replace Function and any[].
- Task 4
groupBy: groups keyed by the key type
groupBy(list, keyOf) puts the items in groups by the key keyOf gives each one: for orders A1 placed, A2 delivered and A3 placed, groupBy(orders, (o) => o.status) is { placed: [A1, A3], delivered: [A2] }, and groupBy of an empty list is {}.
Its type must follow the call: inside keyOf, the item is an Order without writing it; the result's keys are exactly what keyOf can return (for orders, the statuses, so byStatus.lost is a type error); number keys work too. A status with no orders has no group, so reading byStatus.delivered gives Order[] | undefined.
- Task 5 · interview question
Interview: assertNever, and the status that was forgotten
assertNever(value) is for the case that cannot happen: it accepts only never, its return type is never, and it throws, so assertNever('lost' as never) throws 'Unexpected value: lost'. Calling it with a status that has not been handled is then a type error.
With that, typecheck points at statusLabel: it has no case for 'cancelled'. Add it, reading 'Cancelled', next to 'Order placed', 'Brewing your chai' and 'Delivered'. This task also checks that the whole of exercise.ts has no type errors. Dev will ask how assertNever found the missing status.
What you will learn
- Types that follow the arguments: type parameters inferred from the call, K extends keyof T, and one small cast inside a helper.
- Keeping a function's parameters: a type parameter for a whole parameter list.
- One step into the next: type parameters that chain, and overloads for each length.
- Honest results: Partial<Record<K, V>> for groups that may be missing, and never for the case that cannot happen.
More in The interview
Build "Real functions, perfect types" 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.