TypeScriptUtility types

Exclude<UnionType, ExcludedMembers>

Removes from a union the types that match a given type.

On this page

Example

#
TypeScript
type Status = 'draft' | 'review' | 'published' | 'archived'
type EditableStatus = Exclude<Status, 'published' | 'archived'>
// type EditableStatus = "draft" | "review"

const statuses: EditableStatus[] = ['draft', 'review']
console.log(statuses.join(' / '))
Result
draft / review

Definition and usage

#

Exclude<Union, Excluded> returns a union without the members that match Excluded. Exclude<"a" | "b" | "c", "a"> gives "b" | "c".

It works on unions, not on object fields, which is what sets it apart from Omit. Under the hood it’s a conditional type that returns never for matching members.

It’s useful when you want to subtract a few values from a list of possible ones, for example picking from all statuses only the ones a user is allowed to set.

Syntax

#
Syntax
type New = Exclude<Union, ExcludedTypes>

Type parameters

#
  • UnionType

    union

    The union you are removing types from.
  • ExcludedMembers

    type

    The types to remove.

Good practices

#
  • Omit removes fields from an object, and Exclude removes members from a union.
  • Exclude<T, null | undefined> is the same as NonNullable<T>.

Related entries

#

Spotted a mistake or missing an example? Write to us.