object-transforms

Exclude<T, U>

Filters a union T by removing every member that is assignable to U. Distributes over union members via the conditional type, producing the set-difference T \ U.

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

Usage patterns

Pattern Purpose
Exclude<T, null | undefined> Equivalent to NonNullable<T>
Exclude<keyof T, K> Compute complementary key set (basis of Omit)
Exclude<T, Function> Remove function members from a mixed union
Exclude<T, never> Identity — returns T unchanged

Examples

type Fruit = 'apple' | 'banana' | 'cherry';
type NotBanana = Exclude<Fruit, 'banana'>;

Result: 'apple' | 'cherry'

type T = string | number | boolean;
type NonBool = Exclude<T, boolean>;

Result: string | number

type Keys = Exclude<keyof User, 'password'>;

Internal building block of Omit — enumerates safe keys

type X = Exclude<'a' | 'b', 'a' | 'b' | 'c'>;

Result: never — every member of T is assignable to U

Gotcha

Exclude only works on union types — a plain object type passed as T will not be reduced. The distributive behavior can also over-fire on generics; wrap the parameter in a tuple `[T] extends [U]` to disable distribution.

Related utility types

← All TS utility types · TypeScript vs JavaScript · any vs unknown vs never