Handbooks / TypeScript / Chapter 5
Utility and Mapped Types
7 pages · ~5 min✓ Reviewed
Builds on Generics.
Type Transformation Mental Model
1. Types as Functions
- Utility type = generic type-level function: type in, new type out
- Erased at compile time: zero runtime cost
2. The Core Set
| Utility | Effect |
|---|---|
Partial<T> | every property optional |
Required<T> | strips ?, all required |
Readonly<T> | every property readonly |
Pick<T, K> | keeps only keys K |
Omit<T, K> | drops keys K |
PickandOmitare built from mapped types
3. Shallow Only
Partial<User>does not make nested objects optionalReadonly<T>does not freeze nested properties
typescript
type User = { name: string; address: { city: string } }; const u: Readonly<User> = { name: 'A', address: { city: 'X' } }; u.name = 'B'; // error u.address.city = 'Y'; // allowed
only the top level is readonly
Utility Types in Everyday Use
1. Reshaping User
Partial<User>aspatch: any subset of User fieldsOmit<User, 'password'>hides sensitive fieldsPick<User, 'id' | 'name'>whitelists instead
typescript
function update(id: string, patch: Partial<User>) {} type PublicUser = Omit<User, 'password'> type Card = Pick<User, 'id' | 'name'>
2. Record<K, V>
Function Types: ReturnType, Parameters, infer
1. Extracting Types
ReturnType<typeof fn>extracts the return type- Pass
typeof fn, notfn:ReturnType<fn>is an error Parameters<typeof fn>gives the arguments as a tupleParameters<typeof fn>[0]is the first argument
- Async functions return
Promise<T>, so unwrap with Awaited<...>
typescript
async function load(id: string) { return user } type R = ReturnType<typeof load> // Promise<User> type U = Awaited<R> // User
Mapped Types and Conditional Types
1. Mapped Types
{ [K in keyof T]: T[K] }loops over the keys of TPartial<T>={ [K in keyof T]?: T[K] }-?removes optional (Required<T>);-readonlystrips readonly- as renames keys; mapping a key to
neverdrops it
typescript
type Getters<T> = { [K in keyof T as `get${Capitalize<K & string>}`]: () => T[K] }
renames name to getName
2. Conditional Types
satisfies and Common Mistakes
1. What satisfies Does
- satisfies (TS 4.9) checks a value against a type
- It does not widen the value's inferred type
redstaysnumber[], not the union
typescript
const c = { red: [255, 0, 0], green: "#0f0", } satisfies Record<string, number[] | string>; c.red.map(n => n / 2); // number[] c.green.toUpperCase(); // string
each property keeps its own type
2. Annotation vs satisfies
Cheat Sheet: Utility and Advanced Types
1. Utility Types
| Type | Does |
|---|---|
Partial<T> | all optional |
Required<T> | all required |
Readonly<T> | all readonly |
Pick<T, 'a' | 'b'> | keeps those keys |
Omit<T, 'a'> | drops keys |
Check yourself
type A = Omit<{ id: number; name: string }, 'nme'> compiles. What is A?
- Still
{ id: number; name: string } - Omit does not check that K is a key of T, so the typo drops nothing
type A<T> = T extends unknown ? T[] : never and type B<T> = [T] extends [unknown] ? T[] : never. What are A and B for string | number?
- A gives
string[] | number[], because a naked T distributes over the union - B gives
(string | number)[], because the tuple wrap stops distribution