TypeScript 7 reference · Utility Types
TypeScript Exclude<UnionType, ExcludedMembers> Utility Type
Remove members from a union type. Exclude<'a' | 'b' | 'c', 'a'> is 'b' | 'c'.
TypeScript 7 Since 2.8
Syntax
Exclude<UnionType, ExcludedMembers>
Parameters
| Parameter | Description |
|---|---|
UnionType |
The union to filter. |
ExcludedMembers |
Members to remove. |
Return value
The remaining union members.
Examples
type Event = "click" | "scroll" | "keydown" | "keyup";
type MouseEvent = Exclude<Event, "keydown" | "keyup">;
const e: MouseEvent = "scroll";
// @ts-expect-error: keydown was excluded
const k: MouseEvent = "keydown";
console.log(e);
Output
scroll
type Primitive = string | number | boolean | null;
type NotNull = Exclude<Primitive, null>;
const values: NotNull[] = ["a", 1, true];
console.log(values);
Output
[ 'a', 1, true ]
Version history
| Version | Change |
|---|---|
| TypeScript 2.8 | Added |
Related
Extract<Type, Union>· Keep only the members of a union that are assignable to another type. The opposite of Exclude.Omit<Type, Keys>· Build a type with all properties of another type except the ones you list. The opposite of Pick.NonNullable<Type>· Remove null and undefined from a type, for values you've already checked or that can never be missing.- TypeScript Utility Types · tutorial