Conditional Types

Exclude<T, U>

Constructs a type by excluding from T all union members that are assignable to U.

Definition

TypeScript
type Exclude<T, U> = T extends U ? never : T;

Examples

Filtering Union Members
type Status = "active" | "inactive" | "banned" | "pending";

type ActiveStatus = Exclude<Status, "banned" | "inactive">;
// "active" | "pending"

const status: ActiveStatus = "active"; // OK
// const bad: ActiveStatus = "banned"; // Error
Removing null and undefined
type MaybeString = string | null | undefined;

type DefiniteString = Exclude<MaybeString, null | undefined>;
// string

function process(value: DefiniteString) {
  console.log(value.toUpperCase()); // Safe, no null check needed
}
Discriminated Union Filtering
type Event =
  | { type: "click"; x: number; y: number }
  | { type: "keypress"; key: string }
  | { type: "scroll"; offset: number };

type NonScrollEvent = Exclude<Event, { type: "scroll" }>;
// { type: "click"; ... } | { type: "keypress"; ... }

function handleInput(event: NonScrollEvent) {
  console.log(event.type); // "click" | "keypress"
}

Common Use Cases

  • 1Narrowing union types by removing specific members
  • 2Filtering out null and undefined from union types
  • 3Creating subsets of discriminated unions
  • 4Building type-level filters for conditional logic
  • 5Removing internal-only event types from public APIs

Understanding Exclude<T, U>

Exclude<T, U> is a conditional utility type that removes from T all union members assignable to U. It leverages TypeScript's distributive conditional types: when T is a union, the condition is applied to each member individually, and those matching U are replaced with never (effectively removed).

The most common use of Exclude is filtering union types. Given a union of string literals representing states or events, you can derive a narrower union by excluding certain members. This is cleaner and more maintainable than manually defining the subset, because adding a new member to the original union automatically includes it in the excluded type (unless it matches U).

Exclude also works with complex types, not just primitives. You can exclude specific object shapes from a discriminated union. For example, if you have a union of event types, Exclude<Events, { type: "internal" }> removes events marked as internal, producing a public event type.

The relationship between Exclude and Extract is complementary: Exclude keeps members that do NOT match U, while Extract keeps only those that DO. Together, they let you split any union into two disjoint parts.

Exclude is the foundation of the Omit utility type. Omit<T, K> is defined as Pick<T, Exclude<keyof T, K>>, first using Exclude to remove the unwanted keys from keyof T, then using Pick to construct the resulting object type. Understanding Exclude helps you understand the type algebra that powers many advanced TypeScript patterns.

Related Types

More Conditional Types

Explore TypeScript Types

Browse our complete reference of 30 TypeScript utility types with definitions, examples, and explanations.