What if you could instantly remove unwanted types without any mistakes or extra work?
Why Exclude type in Typescript? - Purpose & Use Cases
Imagine you have a list of different fruit types, but you want to create a new list that excludes one specific fruit, like "banana". Doing this manually means checking each fruit one by one and removing the unwanted ones.
This manual way is slow and easy to mess up, especially if the list is long or changes often. You might forget to remove some, or accidentally remove the wrong ones. It's like trying to pick out one color of jellybean from a huge jar by hand.
The Exclude type in TypeScript lets you automatically create a new type that leaves out certain unwanted types. It's like having a magic filter that removes the exact things you don't want, without any mistakes or extra work.
type Fruit = 'apple' | 'banana' | 'orange'; type WithoutBanana = 'apple' | 'orange'; // manually written
type Fruit = 'apple' | 'banana' | 'orange'; type WithoutBanana = Exclude<Fruit, 'banana'>;
This lets you easily create precise types by removing unwanted parts, making your code safer and easier to maintain.
For example, if you have a user role system with roles like 'admin', 'editor', and 'viewer', but want to create a type for all roles except 'admin', Exclude does this perfectly without errors.
Manually removing types is slow and error-prone.
Exclude type automatically filters out unwanted types.
This makes your TypeScript code cleaner and safer.
Practice
Exclude utility type do?Solution
Step 1: Understand the purpose of
ExcludeExcludeis designed to remove certain types from a union type.Step 2: Compare with other utility types
Unlike adding or converting types,Excludespecifically filters out unwanted types.Final Answer:
Removes specific types from a union type -> Option CQuick Check:
Exclude= Remove types [OK]
- Thinking it adds types instead of removing
- Confusing with Partial or Readonly utility types
- Assuming it converts types to strings
'b' from the union 'a' | 'b' | 'c'?Solution
Step 1: Check the syntax of
The correct syntax isExcludeExclude<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 BQuick Check:
Exclude syntax = Exclude<Union, TypeToRemove> [OK]
- Using array syntax for second parameter
- Omitting the second parameter
- Excluding a type not in the union
type Letters = 'x' | 'y' | 'z'; type Result = Exclude;
Solution
Step 1: Identify the original union type
Lettersis'x' | 'y' | 'z'.Step 2: Apply
RemovingExcludeto remove'y'and'z''y'and'z'leaves only'x'.Final Answer:
'x' -> Option AQuick Check:
Exclude removes 'y' and 'z', leaving 'x' [OK]
- Thinking it keeps excluded types
- Confusing with intersection types
- Assuming result is never when some types remain
type Colors = 'red' | 'green' | 'blue'; type NoGreen = Exclude<Colors>;
Solution
Step 1: Check the
ExcludesyntaxExcluderequires two parameters: the union type and the type(s) to exclude.Step 2: Identify the missing parameter
The code only provides one parameter, missing the second one to exclude.Final Answer:
Missing second parameter in Exclude -> Option AQuick Check:
Exclude needs two types: Union and Excluded [OK]
- Forgetting the second parameter
- Assuming Exclude works with one argument
- Confusing Exclude with other utility types
Filtered?type Status = 'success' | 'error' | 'pending' | 'cancelled'; type Filtered = Exclude;
Solution
Step 1: Identify the original union and excluded types
Statusis'success' | 'error' | 'pending' | 'cancelled'. We exclude'error','cancelled', and'unknown'.Step 2: Remove excluded types from the union
'error'and'cancelled'are removed.'unknown'is not inStatus, so it has no effect.Final Answer:
'success' | 'pending' -> Option DQuick Check:
Exclude removes only matching types from union [OK]
- Including excluded types in the result
- Adding excluded types not in the union
- Assuming result is never if unknown type excluded
Ready to prove it?
Pass the test and this pattern moves unknown
