Sub topic 8
Rebuilding the built-in utility types
This chapter rebuilds the most common TypeScript utility types, such as Partial, Pick, Exclude, and ReturnType, from the mapped and conditional type tools. You should already know object types, unions, generics with constraints, and type aliases. Each helper here uses keyof, indexed access (T[K]), extends checks, and infer, so those ideas are assumed.
The pattern behind the utilities
The built-in utilities are not special. Each one is a mapped type or a conditional type with a generic parameter. Once you see the shape, you can read any of them.
The first step is to recognize which tool a utility uses:
- A mapped type loops over keys, written
[K in ...]. Use it when you change properties. - A conditional type picks a branch, written
T extends U ? X : Y. Use it when you filter or extract from a union or function type.
Partial and Required
Partial<T> makes every property optional. The mapped type loops over keyof T and adds ?.
type MyPartial<T> = { [K in keyof T]?: T[K] };
type MyRequired<T> = { [K in keyof T]-?: T[K] };
type Todo = { title: string; done?: boolean; id: number };
type Draft = MyPartial<Todo>; // title?, done?, id?
type Final = MyRequired<Todo>; // title, done, id (all required)
const draft: Draft = { title: "Write chapter" }; // validThe -? modifier removes optionality. Note that MyRequired also removes the ? from done, so done becomes required with type boolean. The T[K] part keeps each original value type.
Readonly, Pick, and Record
Readonly<T> adds readonly to every property. Pick<T, K> keeps only the keys you name. Record<K, V> builds an object whose keys come from a union.
type MyReadonly<T> = { readonly [K in keyof T]: T[K] };
type MyPick<T, K extends keyof T> = { [P in K]: T[P] };
type MyRecord<K extends keyof any, V> = { [P in K]: V };
type Summary = MyPick<Todo, "title" | "id">; // { title: string; id: number }
type Status = MyRecord<"open" | "closed", number>;
// { open: number; closed: number }The constraint K extends keyof T in MyPick stops you from picking a key that does not exist. Without it, T[P] would fail to compile.
Exclude, Extract, and ReturnType
Exclude and Extract filter a union. Both are conditional types. The naked T makes them distribute over the union, which means each member is checked on its own.
type MyExclude<T, U> = T extends U ? never : T;
type MyExtract<T, U> = T extends U ? T : never;
type Mode = MyExclude<"read" | "write" | "admin", "admin">; // "read" | "write"
type Strings = MyExtract<string | number | boolean, string>; // string
type MyReturnType<T extends (...args: any) => any> =
T extends (...args: any) => infer R ? R : any;
type Loaded = MyReturnType<() => Promise<string>>; // Promise<string>infer R captures the return type of the function so the true branch can use it. The constraint on T makes sure you only pass function types.
flowchart TD
A[Generic type T] --> B{Change properties or filter a union?}
B -->|Change properties| C[Mapped type: K in keyof T]
B -->|Filter or extract| D[Conditional type: T extends U]
C --> E[Partial, Required, Readonly, Pick, Record]
D --> F[Exclude, Extract, ReturnType]caption: How the built-in utilities split into mapped and conditional types. alt: A flowchart that starts at a generic type T and asks whether the goal is to change properties or to filter a union. Changing properties leads to a mapped type, which produces Partial, Required, Readonly, Pick, and Record. Filtering leads to a conditional type, which produces Exclude, Extract, and ReturnType.
Common mistakes
A few details cause most errors when rebuilding these types.
- Forgetting
-?inRequired. Without it, the result keeps the optional markers. - Expecting
Excludeto behave the same on a wrapped type. If you write[T] extends [U], distribution turns off, and the filter applies to the whole union at once. - Using
anyinReturnTypewithout a constraint. The constraint keeps the parameter a function type.
> Wrapping the checked type in a tuple turns distribution off. Use this only when you want the whole union checked at once.
Replace the block quote above with a callout in your own version. A callout looks like this:
Exercise
Exercise
What does MyExclude<"a" | "b" | "c", "b"> produce, and why?
Show answer
It produces "a" | "c". The conditional type distributes over each union member: "a" extends "b" is false, so "a" stays; "b" extends "b" is true, so it becomes never and is removed; "c" stays. Union members that become never disappear.