- Abstract class
- Annotations
- Array
- Asserts
- Casting
- Class
- Conditional types
- Const
- Date object
- Decorators
- Default parameter
- Dictionary
- Enum
- Exclude type
- Extract type
- For loop
- forEach()
- Function
- Generics
- Index signature
- Infer
- Inheritance
- Interface
- Keyof type operator
- Let
- Map type
- Mixin
- Module
- Namespace
- Never
- Object type
- Omit type
- Operator
- Optional parameter
- Partial type
- Pick type
- Promise
- Property
- Readonly type
- Record type
- Required type
- Satisfies operator
- Tuples
- Type alias
- Type assertion
- Type guard
- Type narrowing
- typeof Type Operator
- Union
- Utility types
- Var
- Void
TYPESCRIPT
TypeScript Exclude Type: Syntax, Use Cases, and Examples
The TypeScript exclude
utility type helps you narrow down or remove specific types from a union. This is extremely useful when you want to control which values or types are allowed in a variable or property. In exclude TypeScript
, you can subtract one or more types from a broader union, resulting in a refined, more specific type definition.
This guide covers the syntax, behavior, and use cases of the Exclude<T, U>
utility. You'll also learn how to exclude literal types, enum values, object properties, and union types using different strategies and custom utilities in TypeScript.
What Is TypeScript Exclude?
The Exclude<T, U>
utility type constructs a type by excluding from union type T
all types that are assignable to U
.
In simpler terms, it subtracts the second type from the first.
Internal definition:
type Exclude<T, U> = T extends U ? never : T;
If a member of T
is assignable to U
, it's replaced with never
, and all the rest are retained.
Syntax of Exclude TypeScript
The syntax is:
Exclude<UnionType, ExcludedType>
Example:
type Status = "active" | "inactive" | "pending";
type ActiveOnly = Exclude<Status, "inactive" | "pending">;
Now ActiveOnly
is:
"type ActiveOnly = 'active';"
This lets you define variables or fields that specifically exclude the removed values.
Why Use TypeScript Exclude?
Using exclude TypeScript
gives you finer control over:
- Union types that need to eliminate edge cases or forbidden values
- Enums where certain values are not applicable in a specific context
- Object types from which you want to remove keys or nested values
- API responses or schemas with dynamic filtering logic
You avoid writing repetitive and brittle types manually and gain greater flexibility for type-safe logic branching and inference.
Exclude Type from Union Type TypeScript
One of the most common uses of Exclude
is refining union types.
Example:
type Roles = "admin" | "editor" | "guest";
type PublicRoles = Exclude<Roles, "admin">; // "editor" | "guest
This makes it easy to represent all roles except "admin".
Exclude Enum TypeScript
Enums are often used with Exclude
to narrow down acceptable values.
Example:
enum UserRole {
Admin = "admin",
Editor = "editor",
Guest = "guest"
}
type UserRoleKeys = keyof typeof UserRole; // "Admin" | "Editor" | "Guest"
type ExcludedRole = Exclude<UserRole, UserRole.Admin>; // "editor" | "guest"
This allows you to filter enum values for specific use cases like permissions or interface rendering.
You can also work with enum keys if needed:
type DisplayableRoles = Exclude<keyof typeof UserRole, "Admin">;
Exclude Property from Object Type TypeScript
While Exclude
works directly with union types, you can also use it in combination with Omit
to exclude properties from object types.
Example:
type User = {
id: string;
name: string;
password: string;
};
type PublicUser = Omit<User, "password">;
But you can also use Exclude
to dynamically build property keys to omit:
type KeysToRemove = Exclude<keyof User, "password">;
type PublicUserDynamic = Pick<User, KeysToRemove>;
Here, Exclude
helps compute the remaining keys before they’re used with Pick
.
Exclude in TypeScript Conditional Types
Since Exclude
is a conditional type, you can compose it with other logic.
Example:
type Event = "click" | "hover" | "focus" | "scroll";
type AnimationEvents = Exclude<Event, "scroll">;
This is helpful when working with UI types, where certain properties or behaviors should not apply under specific conditions.
How to Exclude Multiple Types
You can exclude multiple values at once by using another union as the second parameter.
type Color = "red" | "green" | "blue" | "yellow";
type PrimaryColors = Exclude<Color, "yellow">; // "red" | "green" | "blue"
type WarmColors = Exclude<Color, "green" | "blue">; // "red" | "yellow"
This pattern helps you create subsets of types efficiently.
Exclude and Mapped Types
You can build complex types by combining Exclude
with mapped types to filter and reassemble object structures.
Example:
type Settings = {
theme: string;
layout: string;
debug: boolean;
};
type Keys = keyof Settings;
type WithoutDebug = Exclude<Keys, "debug">;
type UISettings = {
[K in WithoutDebug]: Settings[K];
};
This results in an object type with just theme
and layout
, removing debug
dynamically.
Exclude in Generic Utility Functions
You can also use exclude TypeScript
in generic utility functions to infer and refine type parameters.
Example:
function filterOut<T, K extends T>(values: T[], toExclude: K[]): Exclude<T, K>[] {
return values.filter((v) => !toExclude.includes(v as K)) as Exclude<T, K>[];
}
const all = ["a", "b", "c"] as const;
const removed = filterOut(all, ["b"]); // type: ("a" | "c")[]
Here, TypeScript infers that "b"
has been excluded from the output type.
Exclude Property from Union of Objects
If you want to remove a specific shape or object variant from a union, you can use Exclude
at the object level.
type Action =
| { type: "login"; user: string }
| { type: "logout" }
| { type: "refresh" };
type LoginOnly = Exclude<Action, { type: "logout" | "refresh" }>;
This filters the union to include only the login action.
Edge Cases and Behavior
- If you exclude a type not present in the original union, the result is unchanged.
- If you exclude all values, the result is
never
. Exclude
does not work on non-union types unless they extend something specific.
Example:
type A = Exclude<string, number>; // string
type B = Exclude<"a" | "b", "a">; // "b"
type C = Exclude<"a", "a">; // never
Understanding these behaviors helps you avoid unexpected outcomes.
Custom Exclude Utilities
You can build your own exclusion-based utilities using Exclude
as a building block.
Example: Exclude keys that match a value type
type OmitByValue<T, V> = {
[K in keyof T as T[K] extends V ? never : K]: T[K];
};
type Data = {
id: number;
name: string;
metadata: object;
};
type WithoutObjects = OmitByValue<Data, object>; // { id: number; name: string }
This approach uses conditional key mapping and exclusion to create complex filtering utilities.
Summary
The TypeScript exclude
utility type is a powerful tool for refining types by removing specific values, keys, or object shapes. It allows you to subtract literal values from unions, remove keys from types dynamically, and build generic, reusable logic in strongly typed applications.
Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.