The Exclude type helps you remove specific types from a group of types. It makes your code clearer and safer by excluding unwanted types.
When you want to create a new type without certain values from an existing type.
When you want to prevent some values from being used in a function or variable.
When you want to narrow down a type by removing options that don't apply.
When you want to handle only specific cases in your code by excluding others.
Syntax
Typescript
Exclude<UnionType, ExcludedMembers>
UnionType is the original set of types.
ExcludedMembers are the types you want to remove from UnionType.
Examples
This removes 'green' and 'blue' from Colors, so PrimaryColors is just 'red'.
Typescript
type Colors = 'red' | 'green' | 'blue';
type PrimaryColors = Exclude<Colors, 'green' | 'blue'>;
This excludes 2 and 3 from the numbers, leaving 1 and 4.
Typescript
type Numbers = 1 | 2 | 3 | 4;
type WithoutTwoAndThree = Exclude<Numbers, 2 | 3>;
This removes boolean from the union, keeping only string and number.
Typescript
type Mixed = string | number | boolean;
type OnlyStringAndNumber = Exclude<Mixed, boolean>;
Sample Program
This program defines a type Pets with four animals. Then it creates NoFish by excluding 'fish'. The function adoptPet only accepts pets except 'fish'. Trying to adopt 'fish' causes an error.
Typescript
type Pets = 'dog' | 'cat' | 'fish' | 'bird';
type NoFish = Exclude<Pets, 'fish'>;
function adoptPet(pet: NoFish) {
console.log(`You adopted a ${pet}.`);
}
adoptPet('dog');
// adoptPet('fish'); // This line will cause a TypeScript error
OutputSuccess
Important Notes
Exclude works only with union types.
It helps catch mistakes early by preventing unwanted types.
Summary
Exclude removes specific types from a union.
It makes your types more precise and your code safer.
Use it when you want to block certain values from a type.
Practice
(1/5)
1. What does the TypeScript Exclude utility type do?
easy
A. Converts a type to a string
B. Adds new types to a union type
C. Removes specific types from a union type
D. Creates a readonly version of a type
Solution
Step 1: Understand the purpose of Exclude
Exclude is designed to remove certain types from a union type.
Step 2: Compare with other utility types
Unlike adding or converting types, Exclude specifically filters out unwanted types.
Final Answer:
Removes specific types from a union type -> Option C
Quick Check:
Exclude = Remove types [OK]
Hint: Exclude filters out unwanted types from unions [OK]
Common Mistakes:
Thinking it adds types instead of removing
Confusing with Partial or Readonly utility types
Assuming it converts types to strings
2. Which of the following is the correct syntax to exclude type 'b' from the union 'a' | 'b' | 'c'?
easy
A. type Result = Exclude<'a' | 'b' | 'c', ['b']>;
B. type Result = Exclude<'a' | 'b' | 'c', 'b'>;
C. type Result = Exclude<'a' | 'b' | 'c'>;
D. type Result = Exclude<'a' | 'b' | 'c', 'd'>;
Solution
Step 1: Check the syntax of Exclude
The correct syntax is Exclude<UnionType, ExcludedType>.
Step 2: Match the correct exclusion
type Result = Exclude<'a' | 'b' | 'c', 'b'>; correctly excludes 'b' from the union 'a' | 'b' | 'c'.
Final Answer:
type Result = Exclude<'a' | 'b' | 'c', 'b'>; -> Option B