What does Exclude<T, U> do?
1. What Exclude<T, U> does
Exclude<T, U>creates a new type by excluding fromTall types that are assignable toU.
In simpler terms, it "subtracts" one union from another.
Example
type Letters = 'a' | 'b' | 'c';
type WithoutB = Exclude<Letters, 'b'>;
// => 'a' | 'c'The type
'b'is excluded, while'a'and'c'remain.
2. How Exclude works with union types
When T is a union,
TypeScript goes through every member of T
and removes the ones assignable to U.
Example 1
type T = 'a' | 'b' | 'c';
type U = 'a' | 'c';
type Result = Exclude<T, U>;
// 'b'
'a'and'c'were inU, so they are excluded.'b'remains.
Example 2
type Primitive = string | number | boolean;
type NoStrings = Exclude<Primitive, string>;
// number | boolean
stringis excluded, the other types are kept.
3. What happens if U is not part of T
If U does not overlap with T,
then nothing is removed, and the result is simply T.
Example
type T = 'a' | 'b';
type U = 'x' | 'y';
type Result = Exclude<T, U>;
// 'a' | 'b' (unchanged)TypeScript "tries to subtract", but finds no matches, so
Excludechanges nothing.
4. How Exclude is implemented internally
Here is what its real implementation in TypeScript looks like:
type Exclude<T, U> = T extends U ? never : T;Explanation:
- Every member of
Tis checked individually: if it is assignable toU, it is replaced withnever(excluded); - the rest are kept.
5. Example with never
type T = 'a' | 'b';
type U = 'a';
type Result = Exclude<T, U>; // 'b'
// essentially this works like:
type Result =
('a' extends 'a' ? never : 'a') |
('b' extends 'a' ? never : 'b');
// => never | 'b' → 'b'6. The difference between Exclude and Omit
These types are similar in meaning ("remove something"), but they work in different contexts.
| Comparison | Exclude<T, U> | Omit<T, K> |
|---|---|---|
| Works with | union types | objects (interfaces) |
| Removes | types (values from the union) | properties (keys) |
| Input example | `'a' | 'b' |
| Removal example | `Exclude<'a' | 'b' |
| Based on | conditional types (extends) | a combination of Pick + Exclude |
Example of the difference in practice
// Exclude
type Letters = 'a' | 'b' | 'c';
type Result1 = Exclude<Letters, 'b'>;
// 'a' | 'c'
// Omit
interface User {
id: number;
name: string;
email: string;
}
type Result2 = Omit<User, 'email'>;
// { id: number; name: string }
Excludeworks at the level of value types,Omitworks at the level of object keys.
7. Combining with other utilities
Example: excluding part of a union field of an interface
interface ApiResponse {
status: 'success' | 'error' | 'loading';
data: string;
}
type NoLoading = Exclude<ApiResponse['status'], 'loading'>;
// 'success' | 'error'Example: creating a type "without null and undefined"
type Clean<T> = Exclude<T, null | undefined>;
type Value = Clean<string | null | undefined>;
// stringSummary
| Question | Answer |
|---|---|
What does Exclude<T, U> do | Removes from T all types assignable to U |
| How it works with union types | Checks every member and excludes the matching ones |
What if U is not part of T | The result stays the same (T does not change) |
How it differs from Omit | Exclude works with a union, while Omit works with object keys |
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.