TypeScript for real code · Chapter 34 of 36
Harder challenges
By the end of this chapter you can write types that match a pattern from either end, unwrap layer after layer, take strings apart and freeze a type all the way down. 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
You passed Dev's warm-up, and he wipes the whiteboard: "Round two. Same rules: types only, no built-in helpers. This time nothing is one line." These are the challenges that sort the people who have read about infer from the people who can use it, and Tapri's own types need the same moves: an API that returns promises inside promises, codes typed in with stray spaces, a frozen order that still has methods.
One conditional type with one infer is not enough here. Every answer needs a pattern placed carefully, a type that calls itself, or both. Bunty's any passes typecheck and fails every test case, as before.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
TupleToUnion and Last: the elements, and the last one
TupleToUnion<T> is the union of a tuple's elements: TupleToUnion<['chai', 'bun', 40]> is 'chai' | 'bun' | 40, TupleToUnion<['chai']> is 'chai', and TupleToUnion<[]> is never.
Last<T> is the last element of a tuple: Last<['chai', 'bun', 'maska']> is 'maska', Last<[40]> is 40, and Last<[]> is never. T must be a tuple, so Last<'chai'> is a type error. No built-in helpers.
- Task 2
Pop and Flatten: take one off, flatten all the way
Pop<T> is the tuple without its last element, the rest in order: Pop<['chai', 'bun', 'maska']> is ['chai', 'bun'], Pop<['chai']> is [], and Pop<[]> is [].
Flatten<T> flattens nested tuples all the way down: Flatten<['chai', 'bun']> stays ['chai', 'bun'], Flatten<['chai', ['bun', ['maska', [40]]]]> is ['chai', 'bun', 'maska', 40], empty tuples inside disappear (Flatten<[[], [[]], 'chai']> is ['chai']), and Flatten<[]> is [].
- Task 3 · interview question
Interview: MyAwaited, unwrapping every layer
MyAwaited<T> is the value a promise gives: MyAwaited<Promise<string>> is string, MyAwaited<Promise<string | number>> is string | number, and promises inside promises are unwrapped all the way, so MyAwaited<Promise<Promise<Promise<{ total: number }>>>> is { total: number }.
Any thenable counts, not only a real Promise: MyAwaited<PromiseLike<number>> is number, and MyAwaited<Promise<PromiseLike<'brewed'>>> is 'brewed'. T must be a promise or another PromiseLike, so MyAwaited<number> is a type error. Do not use the built-in Awaited. Dev will ask how your type knows when to stop.
- Task 4
TrimLeft and Replace: patterns in strings
TrimLeft<S> removes spaces, tabs and newlines from the start of a string and leaves the end alone: TrimLeft<' masala chai '> is 'masala chai ', TrimLeft<'\n\t chai'> is 'chai', TrimLeft<' '> is '', and TrimLeft<''> is ''.
Replace<S, From, To> replaces the first From in S with To: Replace<'masala chai', 'masala', 'cutting'> is 'cutting chai', Replace<'chai chai', 'chai', 'tea'> is 'tea chai' (only the first), Replace<'chai', 'coffee', 'tea'> is 'chai' (no match), Replace<'masala chai', 'masala ', ''> is 'chai', and an empty From changes nothing: Replace<'chai', '', 'tea'> is 'chai'. S, From and To are strings: without that rule on them, the template pattern is a type error in exercise.ts.
- Task 5 · interview question
Interview: DeepReadonly, frozen all the way down
DeepReadonly<T> makes every property readonly at every level. For an order with id, items (a list of { name; qty }), customer (with a nested address), tags (the tuple ['hot', 'urgent']) and total (() => number): id stays string; customer is { readonly name: string; readonly address: { readonly area: string; readonly pin: number } }; items is readonly { readonly name: string; readonly qty: number }[]; tags is readonly ['hot', 'urgent']; and total stays exactly () => number.
Changing the id, the address's area or an item's qty, and pushing to items, are all type errors. Dev will ask why a function inside the frozen order must stay a function.
What you will learn
- Patterns from either end: a rest element can come first, last or in the middle of a tuple pattern.
- Layers and walks: infer inside a generic type, and a type that calls itself to go one layer deeper or one element further.
- Patterns in strings: unions inside a template pattern, where infer stops, and the empty string.
- Mapped types on arrays and functions: what a mapped type does to a tuple, and to a function.
More in The interview
Build "Harder challenges" 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.