Course contentsShow
← TypeScript
Lesson 714 of 1,79616. Utility TypesFree lesson

Combining Multiple Utilities

Chain utility types together like Partial<Pick<T, K>> to create complex transformations compositionally.

Combining Multiple Utilities

What you'll learn: How to chain utility types together like Partial<Pick<T, K>> to create complex transformations compositionally.


What Is Utility Type Composition?

Just like you can chain array methods (arr.filter(...).map(...)), you can nest utility types to perform multiple transformations in sequence. Each utility takes a type, transforms it, and passes the result to the next layer. This lets you build complex type transformations from simple, reusable pieces.

Think of it like Instagram filters: one filter adjusts brightness, another adds contrast, another applies a vintage effect. Apply them in sequence, and you get a completely transformed photo. Utility types work the same way.


Basic Composition Pattern

The most common pattern is wrapping one utility type inside another:

type User = {
  id: number;
  name: string;
  email: string;
  password: string;
};

// Pick specific fields, then make them optional
type UserUpdate = Partial<Pick<User, 'name' | 'email'>>;
// Equivalent to: { name?: string; email?: string; }

Here, Pick runs first (selecting properties), then Partial makes them optional. Order matters!


Practical Real-World Example

// Database record with metadata
type DbRecord = {
  id: string;
  createdAt: Date;
  updatedAt: Date;
  userId: number;
  status: 'active' | 'inactive';
  data: Record<string, unknown>;
};

// Public API response: omit metadata, make readonly
type ApiResponse = Readonly<Omit<DbRecord, 'createdAt' | 'updatedAt'>>;

You've stripped internal fields, then locked down the shape to prevent modification.


Common Composition Chains

  • Partial<Pick<T, K>> — optional subset of properties
  • Required<Omit<T, K>> — remove fields, then require all remaining
  • Readonly<Record<K, T>> — create a lookup table that's immutable
  • NonNullable<ReturnType<F>> — unwrap a function's return, strip null/undefined

Key Takeaway: Utility types are composable building blocks—chain them together to create precise transformations without repetitive manual type declarations.