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

Combining Utility Types

Chaining utilities like Partial<Readonly<T>> to create types that are both optional and immutable.

Combining Utility Types

You'll learn how to layer multiple utility types together to create precisely constrained types with minimal code.

The Power of Composition

TypeScript's utility types aren't just standalone tools—they can be chained together using standard type syntax. When you wrap one utility inside another, you get the effects of both. This is like applying multiple Instagram filters to a photo: each layer adds its own transformation.

The most common pattern is Partial<Readonly<T>>, which creates a type where all properties are both optional and immutable.

How It Works

When you nest utilities, TypeScript applies them from the inside out:

interface User {
  id: number;
  name: string;
  email: string;
}

// All properties optional AND readonly
type PartialReadonlyUser = Partial<Readonly<User>>;

const config: PartialReadonlyUser = {
  name: "Alice"
};

// config.name = "Bob"; // ❌ Error: readonly
// But you can omit any property ✓

Order Matters (Sometimes)

For Partial and Readonly, the order doesn't change the outcome—Partial<Readonly<T>> equals Readonly<Partial<T>>. Both make properties optional and readonly.

However, when combining with Required, order can matter:

// First make required, then readonly
type Config = Required<Readonly<Partial<User>>>;
// Effectively: Required<Readonly<User>>

Real-World Use Case

This pattern shines in configuration objects where you want immutability after creation, but flexible initialization:

type AppConfig = Partial<Readonly<{
  apiUrl: string;
  timeout: number;
  retries: number;
}>>;

Key Takeaway: Utility types compose naturally—wrap them to combine their effects, creating powerful type constraints without writing custom mapped types.