Literal types and value narrowing
📖 Scenario: You are building a simple TypeScript program to handle user roles in a system. Each user can have a specific role, and you want to make sure the program only accepts certain exact role names. This helps avoid mistakes like typos.
🎯 Goal: Create a TypeScript program that uses literal types to define exact user roles and uses value narrowing to check and print messages based on the user's role.
📋 What You'll Learn
Use a literal type to define user roles as 'admin', 'editor', or 'viewer'.
Create a variable with one of these roles.
Use a function that takes the role and uses value narrowing with a switch statement.
Print a specific message for each role.
💡 Why This Matters
🌍 Real World
Defining exact user roles helps prevent bugs and makes your program safer by only allowing known values.
💼 Career
Many jobs require writing code that handles specific fixed values safely, especially in user management and permissions.
Progress0 / 4 steps