The Extract Utility Type Explained
You'll learn: How Extract<T, U> leverages distribution to keep only union members that are assignable to U.
How Extract Works
Extract<T, U> is a built-in utility type that filters a union type T, keeping only the members that are assignable to (compatible with) U. It's the opposite of Exclude.
The magic happens through distributive conditional types. When TypeScript sees Extract<T, U>, it actually applies this pattern:
type Extract<T, U> = T extends U ? T : never;
Because T is a union and appears naked (not wrapped in an array or tuple) on the left side of extends, TypeScript distributes the conditional across each union member individually.
Distribution in Action
Think of it like a filter: each member of the union T gets checked individually. If it passes the test (extends U), keep it. Otherwise, replace it with never — which TypeScript automatically removes from unions.
type AllValues = "success" | "error" | "pending" | 404 | 500;
// Extract only strings
type StringValues = Extract<AllValues, string>;
// Result: "success" | "error" | "pending"
// Extract only numbers
type NumericValues = Extract<AllValues, number>;
// Result: 404 | 500
// Extract specific literals
type ErrorStates = Extract<AllValues, "error" | "pending">;
// Result: "error" | "pending"
Behind the scenes, TypeScript processes each member: "success" extends string ? "success" : never, then "error" extends string ? "error" : never, and so on. The never results vanish, leaving only matching members.
Why This Matters
Extract lets you carve out type-safe subsets from complex unions without manually listing every matching member. It's pattern matching at the type level.
Key Takeaway: Extract<T, U> uses distributive conditional types to iterate through union T, keeping only members assignable to U and discarding the rest as never.