The Exclude Utility Type Explained
What you'll learn: How Exclude<T, U> leverages distributive conditional types to filter out unwanted types from a union.
You've already seen how conditional types distribute over unions—when you pass a union into a conditional type, TypeScript checks each member independently. The Exclude<T, U> utility type is one of the most elegant demonstrations of this power in action.
How Exclude Works
Exclude<T, U> removes from union type T any types that are assignable to U. Here's its actual definition:
type Exclude<T, U> = T extends U ? never : T;
Because this conditional is distributive, when T is a union, TypeScript evaluates the condition for each member separately. If a member extends U, it becomes never (which vanishes from unions). If it doesn't, it stays as-is.
The Mechanism in Action
When you write Exclude<"a" | "b" | "c", "a">, TypeScript distributes:
type Result =
| ("a" extends "a" ? never : "a") // becomes never
| ("b" extends "a" ? never : "b") // becomes "b"
| ("c" extends "a" ? never : "c"); // becomes "c"
// Result: "b" | "c"
The "a" becomes never and disappears, leaving only "b" | "c".
Practical Examples
Filtering string literals:
type AllStatuses = "pending" | "approved" | "rejected";
type ActiveStatuses = Exclude<AllStatuses, "rejected">;
// "pending" | "approved"
Removing multiple types:
type Mixed = string | number | boolean;
type OnlyString = Exclude<Mixed, number | boolean>;
// string
Key Takeaway: Exclude<T, U> uses distributive conditional types to iterate over union members, replacing those assignable to U with never, effectively filtering them out of the resulting union.