Common Conditional Type Patterns
What you'll learn: How to use conditional types to solve recurring problems like extracting array element types, unwrapping Promises, and filtering object properties.
Why These Patterns Matter
You've learned how conditional types work — now it's time to see the patterns that appear everywhere in real TypeScript code. These aren't just theoretical exercises; they're the building blocks of type-safe libraries and applications.
Pattern 1: Extracting Array Element Types
Want to get the type of what's inside an array?
type Unarray<T> = T extends (infer U)[] ? U : T;
type Strings = Unarray<string[]>; // string
type Numbers = Unarray<number[]>; // number
type NotArray = Unarray<boolean>; // boolean (unchanged)
This pattern checks if T is an array, extracts the element type with infer, and returns it. If it's not an array, we return T unchanged.
Pattern 2: Unwrapping Promises
Similar pattern for async values:
type Awaited<T> = T extends Promise<infer U> ? U : T;
type Resolved = Awaited<Promise<number>>; // number
type AlreadySync = Awaited<string>; // string
This "unwraps" the Promise to reveal what value it will eventually contain.
Pattern 3: Type Filtering
Remove specific types from a union:
type RemoveNull<T> = T extends null ? never : T;
type Clean = RemoveNull<string | null | number>; // string | number
When a conditional returns never, it gets removed from unions — a powerful filtering technique.
Key Takeaway: Common conditional type patterns like extracting array elements, unwrapping Promises, and filtering types with never are the foundation of type-level programming in TypeScript — learn these patterns and you'll recognize them everywhere in real-world code.