📖 Scenario: You are working on a TypeScript project where you have a union type representing different user roles. You want to create a new type that excludes a specific role from this union.
🎯 Goal: Learn how to use the Exclude utility type in TypeScript to remove a specific type from a union type.
📋 What You'll Learn
Create a union type called UserRoles with exact values 'admin', 'editor', and 'viewer'.
Create a new type called NonAdminRoles that excludes the 'admin' role from UserRoles using the Exclude type.
Declare a variable called role of type NonAdminRoles.
Assign the value 'editor' to the variable role.
Print the value of role to the console.
💡 Why This Matters
🌍 Real World
In real projects, you often have union types representing options or roles. Sometimes you need to exclude certain options to create more specific types.
💼 Career
Understanding utility types like <code>Exclude</code> helps you write safer and clearer TypeScript code, which is valuable for frontend and backend development jobs.
Progress0 / 4 steps
1
Create the union type UserRoles
Create a union type called UserRoles with the exact string values 'admin', 'editor', and 'viewer'.
Typescript
Hint
Use the pipe symbol | to combine string literals into a union type.
2
Create the type NonAdminRoles excluding 'admin'
Create a new type called NonAdminRoles that uses the Exclude utility type to remove 'admin' from UserRoles.
Typescript
Hint
The Exclude type takes two parameters: the union type and the type to exclude.
3
Declare a variable role of type NonAdminRoles
Declare a variable called role with the type NonAdminRoles.
Typescript
Hint
Use let to declare the variable with the correct type.
4
Assign and print the value of role
Assign the value 'editor' to the variable role and print it to the console using console.log.
Typescript
Hint
Use console.log(role) to display the value.
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