TypeScript for real code · Chapter 33 of 36
Warm-up challenges
By the end of this chapter you can rebuild small pieces of TypeScript's toolbox, like Pick, Readonly and Exclude, using only types. 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
Dev is hiring before the funding round, and every developer at Tapri takes his interview first, you included. Round one is a set of type challenges, a classic interview round: anyone can use Pick, but writing it shows you know how mapped and conditional types really work.
So far you have used the toolbox as a black box. Here each tool starts as any, and you write it. Bunty left them as any and said he was done: typecheck printed No type errors., and the Check failed every case, because it compares with an exact Equal, and any is never equal to anything.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
MyPick and MyReadonly: rebuild Pick and Readonly
MyPick<T, K> keeps only the keys K of T, with their types: MyPick<Order, 'id' | 'total'> is { id: string; total: number }, and MyPick<Order, 'status'> is { status: 'placed' | 'delivered' }. K must be keys of T, so MyPick<Order, 'id' | 'tip'> is a type error.
MyReadonly<T> makes every property of T readonly and keeps optional ones optional: for { id: string; items: string[]; tip?: number } it is { readonly id: string; readonly items: string[]; readonly tip?: number }, so changing a readonly order's id is a type error. Do not use Pick or Readonly: they are what you are building. The Check compares with an exact Equal, so any fails every case.
- Task 2
TupleToObject and Length: types from a tuple
TupleToObject<T> turns a readonly tuple of keys into an object whose every key maps to itself: for ['cutting', 'regular', 'large'] as const it is { cutting: 'cutting'; regular: 'regular'; large: 'large' }, and for [1, 2] as const it is { 1: 1; 2: 2 }. T must be a tuple of strings, numbers or symbols, so a tuple holding an object is a type error.
Length<T> is the length of a tuple: 3 for those three sizes, and 0 for []. T must be a tuple, readonly ones from as const included, so Length<'chai'> is a type error.
- Task 3 · interview question
Interview: First and Concat, the ends of tuples
First<T> is the first element of a tuple: First<['chai', 'bun']> is 'chai', First<[undefined, 'chai']> is undefined, and First<[]> is never, because an empty tuple has no first element at all. T must be a tuple, so First<'chai'> is a type error.
Concat<T, U> joins two tuples, like [...t, ...u] for values: Concat<[], []> is [], Concat<['chai'], ['bun', 'maska']> is ['chai', 'bun', 'maska'], and a readonly tuple from as const works too (['cutting', 'regular'] as const joined with ['large'] is ['cutting', 'regular', 'large']). Both must be tuples, so Concat<null, ['chai']> is a type error. Dev will ask how First tells an empty tuple apart from one that starts with undefined.
- Task 4
MyExclude and If: types that decide
MyExclude<T, U> keeps the members of the union T that are not assignable to U. From 'placed' | 'brewing' | 'delivered' | 'cancelled', removing 'cancelled' leaves 'placed' | 'brewing' | 'delivered', and removing 'placed' | 'brewing' leaves 'delivered' | 'cancelled'. MyExclude<string | number | (() => void), Function> is string | number. Do not use Exclude.
If<C, T, F> is T when C is true and F when C is false: If<true, 'chai', 'coffee'> is 'chai' and If<false, 'chai', 'coffee'> is 'coffee'. C must be a boolean, so If<null, 'chai', 'coffee'> is a type error.
- Task 5 · interview question
Interview: Includes, with exact matches only
Includes<T, U> is true when the tuple T has an element that is exactly U, and false otherwise. Includes<['chai', 'bun'], 'chai'> is true, Includes<['chai', 'bun'], 'coffee'> is false, Includes<[], 'chai'> is false, and Includes<[1, 'chai', false], false> is true: the match can be anywhere.
Exactly means exactly: Includes<[boolean], false> is false, Includes<[1 | 2], 1> is false, Includes<[{ a: 1 }], { readonly a: 1 }> is false, and Includes<[null], undefined> is false. The Check's Equal is not available in your file, so write your own helper. Dev will ask why extends is not the same as equal.
What you will learn
- Rules on type parameters: extends in the angle brackets, so a wrong argument is a type error.
- Tuples as types: reading elements by index, readonly, spreads.
- Patterns and unions: infer in a pattern, and a conditional type that runs once per member of a union.
- Exactly the same: why extends is not equality, and a type that walks a tuple.
More in The interview
Build "Warm-up 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.