Skip to content

TypeScript Advanced and Utility Types ​

Derive every type from one source of truth, compute types from other types, and know when that power stops paying for itself.

In this chapter: Partial, Pick, Omit and Record · ReturnType, Exclude and friends · mapped types and key remapping · conditional types and infer · template literal types

💡 The Core Idea ​

Every type you write by hand twice will disagree with itself one day. Utility types make one model the source of truth and derive the rest: the create input, the update patch and the public view. Add a field to the model and every derived type follows.

Underneath, the type system is a small language of its own. Mapped types iterate, conditional types branch, and infer pattern-matches. Every utility type is a one-line program in that language. Beside the syntax, learn when to stop. A type that takes ten minutes to read costs more than the bug it prevents.

How It Works ​

Assume one model throughout:

typescript
interface User {
  id: number;
  name: string;
  email: string;
  passwordHash: string;
  role: 'admin' | 'user';
  bio?: string;
}

Deriving shapes from one model ​

typescript
type PublicUser = Pick<User, 'id' | 'name' | 'email'>; // keep a few
type CreateUserInput = Omit<User, 'id' | 'passwordHash'> & { password: string }; // remove a few
type UpdateUserInput = Partial<CreateUserInput>; // every field optional — a PATCH body
type PublicProfile = Readonly<Omit<User, 'passwordHash'>>; // they compose
UtilityEffectWatch for
Partial, Required, ReadonlyChange every property's modifierAll three are shallow
Pick<T, K>Keep the listed keysA key that does not exist is an error
Omit<T, K>Remove the listed keysA key that does not exist is silently ignored
Record<K, V>An object with keys KOver a literal union, every member must appear

The Pick/Omit asymmetry matters. Omit<User, 'pasword'> compiles and quietly omits nothing, so a renamed field leaves a hole. Pick catches the same typo. That is a reason to prefer it when stripping something like passwordHash.

Record<Status, string> over a literal union is an exhaustiveness check for free. Add a status and the label map fails to compile. Record<string, T> is the opposite, because it gives up key checking.

Extracting and filtering ​

typescript
type AuthPayload = ReturnType<typeof getAuthPayload>; // `typeof` turns a value into a type
type Args = Parameters<typeof createPost>; // a labelled tuple
type Fetched = Awaited<ReturnType<typeof fetchUser>>; // User, not Promise<User>

type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE';
type Mutating = Exclude<HttpMethod, 'GET'>; // 'POST' | 'PUT' | 'DELETE'
type Definite = NonNullable<User | null>; // User

Exclude and Extract work on union members. Omit and Pick work on object keys. Reaching for the wrong pair is the most common mix-up here.

Mapped types ​

A mapped type iterates over the keys of a type and transforms each one. Partial is literally { [K in keyof T]?: T[K] }. Knowing that lets you write the variant the standard library lacks.

typescript
// Per-field form state — one rule instead of a hand-written twin type
type FormFields<T> = {
  [K in keyof T]: { value: T[K]; dirty: boolean; error?: string };
};

// Key remapping rewrites the key itself
type Getters<T> = {
  [K in keyof T as `get${Capitalize<string & K>}`]: () => T[K];
};
// Getters<{ name: string }> is { getName: () => string }

Conditional types and infer ​

T extends U ? X : Y is a branch. infer captures a type from the pattern being matched.

typescript
type ArrayElement<T> = T extends readonly (infer E)[] ? E : never;
type Unpacked<T> = T extends Promise<infer U> ? U : T;

// This is how the standard library builds ReturnType
type MyReturnType<T> = T extends (...args: never[]) => infer R ? R : never;

Conditional types distribute over a naked type parameter (a bare T, not wrapped in another type), and that surprises people. ArrayElement<string[] | number[]> is string | number, because the condition runs on each member separately. That is what makes Exclude work. Wrap the parameter in a tuple, as in [T] extends [U], to treat the union as one thing.

Template literal types ​

String types built from patterns turn APIs that pass loose strings into checked ones:

typescript
type Route = `/${'users' | 'posts'}` | `/${'users' | 'posts'}/:id`;

type AppEvents = {
  'user:login': { userId: number };
  'order:placed': { orderId: string; total: number };
};

function emit<K extends keyof AppEvents>(event: K, payload: AppEvents[K]): void {}

emit('user:login', { userId: 1 }); // ✅
emit('user:login', { userId: '1' }); // ❌ wrong payload for this event

Combinations multiply. Two unions of four give sixteen types, which is fine. Four unions of ten give ten thousand, and the compiler will say so.

When to Use It ​

ScenarioReach forWhy
A PATCH endpoint bodyPartial<Model>Every field optional, none invented
An API view that must not leak a fieldPick<Model, …>A typo in the key list fails the build
A lookup that must cover every union memberRecord<Union, V>Missing a member is a compile error
A wrapper that must match a function's shapeParameters and ReturnTypeThe wrapper cannot drift from the original
Event names, route paths, i18n keysTemplate literal typesA string typo becomes a compile error
A shape used in exactly one placeWrite it out longhandA computed type costs reader time forever

Common Mistakes ​

❌ Trusting Omit to catch a typo. It accepts keys that do not exist. Prefer Pick where a miss leaks a field.

❌ Forgetting typeof. ReturnType<getAuthPayload> is an error. The argument must be a type: ReturnType<typeof getAuthPayload>.

❌ Expecting Readonly or Partial to go deep. They apply to the top level only. Readonly<Config> leaves config.db.host writable.

❌ Forgetting that conditional types distribute. A check you meant for the whole union runs per member. Use [T] extends [U] when you want one check.

❌ Building a type so clever it cannot be read. A five-line conditional type with three infer positions is a maintenance cost. If the alternative is a little duplication, take the duplication.

⚠️ Partial<T> on an update object hides a real ambiguity. { bio: undefined } means "clear the field" and {} means "leave it alone", and Partial types them the same. Model the difference explicitly if the API cares.

⚠️ Deeply recursive types hit the compiler's instantiation limit. They slow every build and every editor keystroke. If tsc --generateTrace points at one of your helpers, simplify it rather than raising limits.

🔑 Key Takeaways ​

  • Deriving types from one model stops the create, update and view shapes drifting apart.
  • Pick rejects a key that does not exist and Omit ignores it — prefer Pick where a miss leaks data.
  • Exclude and Extract filter union members; Omit and Pick filter object keys.
  • Mapped types iterate, conditional types branch, and infer pattern-matches — every utility type is built from them.
  • Conditional types distribute over a naked type parameter; wrap it in a tuple to stop that.

Interview Questions ​

Q: What is the difference between Pick and Omit, beyond direction?

Pick constrains its keys to keyof T, so a misspelling is a compile error. Omit accepts any key, so a misspelling omits nothing. Direction decides which is shorter to write. The asymmetry decides which is safer when the risk is exposing a field you meant to strip.

Q: What does infer do?

It captures a type from inside a conditional type's pattern and names it for the true branch. T extends Promise<infer U> ? U : T matches any promise and gives back its resolved type. ReturnType, Parameters and Awaited are all built on it.

Q: What is distribution in conditional types, and when does it bite?

When the checked type is a naked type parameter and the argument is a union, the condition runs on each member and the results are joined. That is what makes Exclude and NonNullable work. It bites when you meant one check on the whole union. [T] extends [U] turns it off.

Q: When would you not use Partial for an update type?

When "field absent" and "field cleared" must be told apart, because Partial merges them. Also when some fields are required in an update, such as a version number for optimistic locking. Partial<Pick<T, …>> & { version: number } says that, and Partial<T> does not.

The whole book, free to read. Generated from the manuscript on every change.

Your progress

Tick each chapter in the sidebar once you have read and understood it. Your name and email keep the ticks on any device.

Your name and email are stored to save your progress. Nothing else is done with them.