Capstone: build utility types from scratch
Go from the first annotated variable to writing your own conditional, mapped and infer-based utility types, while keeping a clear line between compile-time checking and runtime behavior.
What you will be able to do
#- Reimplement Pick, Omit and DeepPartial using mapped and conditional types.
- Prove type behavior with compile-time assertions.
- Choose between a type-level and a runtime solution.
Overview
#The goal
Rebuild a small standard library of utility types and prove each one with compile-time assertions. This is the skill interviewers probe when they ask you to write DeepPartial or Pick from scratch.
Type-level assertions
The pattern below checks type equality without runtime code.
type Equal<A, B> =
(<T>() => T extends A ? 1 : 2) extends
(<T>() => T extends B ? 1 : 2) ? true : false;
type Expect<T extends true> = T;
type Check = Expect<Equal<MyPick<{ a: number; b: string }, "a">, { a: number }>>;
If the two types differ, Expect receives false, which does not satisfy T extends true, and the file fails to compile.
Building blocks
Pick<T, K>is a mapped type overK.Omit<T, K>combinesPickwithExclude.DeepPartial<T>recurses, so it needs a conditional to stop on primitives.
type DeepPartial<T> = T extends object
? { [K in keyof T]?: DeepPartial<T[K]> }
: T;
Compile-time versus runtime
These types produce no JavaScript. A DeepPartial type lets a caller pass a nested subset, but the function that merges it still needs real runtime logic to combine the objects. The type proves the shape; the runtime code performs the merge.
Where to go next
The remaining course targets extend this into declaration files, module augmentation, variance and performance of recursive types.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Implement
Implement `MyPick<T, K extends keyof T>` from scratch with a mapped type, without using the built-in Pick.
starter.typescriptexport type MyPick<T, K extends keyof T> = { [P in keyof T]: T[P] };Reveal the reference answer
reference.typescriptexport type MyPick<T, K extends keyof T> = { [P in K]: T[P] };Authored expected output: Compile-time: MyPick<{ a: number; b: string }, "a"> is { a: number }. No runtime output.
The mapped type iterates the selected keys and copies their value types.
Exercise 2Implement
Implement `DeepPartial<T>` that makes every nested property optional while leaving primitives unchanged.
starter.typescriptexport type DeepPartial<T> = T extends object ? { [K in keyof T]?: T[K] } : T;Reveal the reference answer
reference.typescriptexport type DeepPartial<T> = T extends object ? { [K in keyof T]?: DeepPartial<T[K]> } : T;Authored expected output: Compile-time: DeepPartial<{ a: { b: number } }> is { a?: { b?: number } }. No runtime output.
The conditional stops recursion on non-objects, and the mapped type recurses into each property.
Exercise 3Test
Write compile-time assertions that prove `MyPick` and `DeepPartial` behave correctly, using an Equal helper and an Expect helper.
starter.typescriptexport type Equal<A, B> = (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? true : false; export type Expect<T extends true> = T; export type MyPick<T, K extends keyof T> = { [P in K]: T[P] }; export type DeepPartial<T> = T extends object ? { [K in keyof T]?: DeepPartial<T[K]> } : T; // add assertions hereReveal the reference answer
reference.typescriptexport type Equal<A, B> = (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? true : false; export type Expect<T extends true> = T; export type MyPick<T, K extends keyof T> = { [P in K]: T[P] }; export type DeepPartial<T> = T extends object ? { [K in keyof T]?: DeepPartial<T[K]> } : T; export type PickCheck = Expect<Equal<MyPick<{ a: number; b: string }, "a">, { a: number }>>; export type DeepCheck = Expect<Equal<DeepPartial<{ a: { b: number } }>, { a?: { b?: number } }>>;Authored expected output: Compile-time only: the file compiles when both assertions hold and fails when either type is wrong. No runtime output.
The assertions turn type behavior into a compile-time test suite.
Exercise 4Interview
An interviewer asks for a DeepPartial type and a function that applies a partial patch. Explain what the type guarantees, what it cannot guarantee, and where the runtime code must take over.
Reveal the reference answer
reference.typescriptThe type guarantees that a caller may omit any nested field and that any field they do provide matches the original type. It cannot guarantee the values are correct at runtime, that the patch is applied, or that omitted fields are preserved. The merge function must perform the real recursive copy, handle arrays and null deliberately, and decide whether undefined means keep or delete. The type proves the shape; the runtime code proves the behavior, and both need tests.Authored expected output: A written answer separating the compile-time guarantee from the runtime responsibility.
This is the core lesson of the course: types describe, runtime code enforces.
Common misconceptions
#- A DeepPartial type deep-merges values at runtime.
- Type assertions execute code.
- Recursive types have no base case.
Follow-up practice
#- typescript-tuples-readonly
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.