Intersection with Utility Types
You'll learn how to combine intersection types with TypeScript's built-in utility types to create powerful, flexible type transformations from multiple sources.
The Core Idea
You've seen how intersection types (&) merge multiple types together. Now imagine combining that merging power with utility types like Partial<T>, Readonly<T>, or Pick<T>. This lets you take pieces from different types, apply transformations to them, and combine the results into exactly the shape you need.
Think of it like building a custom meal: you start with base ingredients (your original types), apply different preparations to each (the utility types), then combine them on one plate (the intersection).
Basic Pattern
The syntax looks like this:
type BaseConfig = { apiKey: string; timeout: number };
type UserPrefs = { theme: string; notifications: boolean };
// Combine a partial base config with full user preferences
type AppSettings = Partial<BaseConfig> & UserPrefs;
// Now apiKey and timeout are optional, but theme and notifications are required
const settings: AppSettings = {
theme: "dark",
notifications: true
// apiKey and timeout can be omitted
};
Practical Combinations
type Product = { id: number; name: string; price: number };
type Review = { rating: number; comment: string };
// Readonly product info intersected with mutable review
type ProductWithReview = Readonly<Product> & Review;
// Pick specific fields and combine with another type
type ProductSummary = Pick<Product, "name" | "price"> & { available: boolean };
Key Takeaway: Intersection with utility types lets you selectively transform different parts of your types before merging them, giving you fine-grained control over the final shape without modifying the originals.