Syntax
Exclude<UnionType, ExcludedMembers>
Extract<UnionType, Union>Examples
Exclude<T> - Removing Union Members
Deriving a smaller union by removing specific options.
type Status = "pending" | "active" | "completed" | "cancelled";
type ActiveStatus = Exclude<Status, "cancelled">;
// ActiveStatus is: "pending" | "active" | "completed"
type NonNullableString = Exclude<string | null | undefined, null | undefined>;
// NonNullableString is: stringExtract<T> - Keeping Only Matching Members
The inverse operation - keeping just the members that match.
type AllTypes = string | number | boolean | (() => void);
type OnlyFunctions = Extract<AllTypes, Function>;
// OnlyFunctions is: () => void
type OnlyPrimitives = Extract<AllTypes, string | number | boolean>;
// OnlyPrimitives is: string | number | booleanBest practices
- Use Exclude<T> to derive a narrower union type by removing specific known members, keeping it in sync if the original union changes
- Use Extract<T> when you need to filter a broad union down to just the members matching a certain shape or category
- Both operate purely on union types - they have no effect on object property removal, which is what Omit is for instead
- Combine with typeof and a literal array (as const) to derive both a runtime array of values and a matching union type from a single source
At a glance
- Purpose
- Static types for JavaScript
- File extension
- .ts ยท .tsx
- Runs in
- Compiled to JavaScript
- Usually used with
- JavaScript and its ecosystem
Specifications & further reading
Related TypeScript documentation
Partial<T> and Required<T>
Partial<T> constructs a new type with every property of T marked optional - useful for representing partial updates, like a PATCH request body. Required<T> does the opposite, making every property mandatory even if the original type had optional ones, useful for ensuring a fully-populated object at a specific point in your code.Pick<T> and Omit<T>
Pick<T, Keys> constructs a new type by selecting only the specified properties from T. Omit<T, Keys> does the reverse, constructing a new type with all properties of T except the specified ones. Both are extremely common for deriving smaller, focused types from a larger base type without duplicating the definition.Record<K, V>
Record<Keys, ValueType> constructs an object type with a specific set of keys, all mapped to the same value type. It is the concise, standard way to type dictionary-like objects, and is often clearer than writing an equivalent index signature by hand.Readonly<T>
Readonly<T> constructs a new type where every property of T is marked readonly, preventing reassignment after the object is created. It is a shallow transformation - nested objects within a Readonly<T> are not automatically deep-frozen, only the top-level properties are protected from reassignment.
Partial<T> constructs a new type with every property of T marked optional - useful for representing partial updates, like a PATCH request body. Required<T> does the opposite, making every property mandatory even if the original type had optional ones, useful for ensuring a fully-populated object at a specific point in your code.Pick<T> and Omit<T>
Pick<T, Keys> constructs a new type by selecting only the specified properties from T. Omit<T, Keys> does the reverse, constructing a new type with all properties of T except the specified ones. Both are extremely common for deriving smaller, focused types from a larger base type without duplicating the definition.Record<K, V>
Record<Keys, ValueType> constructs an object type with a specific set of keys, all mapped to the same value type. It is the concise, standard way to type dictionary-like objects, and is often clearer than writing an equivalent index signature by hand.Readonly<T>
Readonly<T> constructs a new type where every property of T is marked readonly, preventing reassignment after the object is created. It is a shallow transformation - nested objects within a Readonly<T> are not automatically deep-frozen, only the top-level properties are protected from reassignment.