Extract Type with TypeScript
📖 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 extracts only the admin roles from this union.
🎯 Goal: Build a TypeScript program that uses the Extract utility type to create a new type containing only the admin roles from a union type.
📋 What You'll Learn
Create a union type called
UserRoles with the exact values: 'admin', 'user', 'guest', 'superadmin'Create a type called
AdminRoles that extracts only 'admin' and 'superadmin' from UserRoles using the Extract utility typeCreate a variable called
adminRole of type AdminRoles and assign it the value 'admin'Print the value of
adminRole to the console💡 Why This Matters
🌍 Real World
Extracting specific types from unions helps keep your code safe and clear when working with different categories or roles in applications.
💼 Career
Understanding TypeScript utility types like <code>Extract</code> is important for writing maintainable and type-safe code in professional frontend and backend development.
Progress0 / 4 steps