Type alias for unions
📖 Scenario: You are building a simple program to categorize different types of pets by their names and types.
🎯 Goal: Create a type alias for a union of pet types, use it in a variable, and print the pet's type.
📋 What You'll Learn
Create a type alias called
PetType that is a union of the strings 'dog', 'cat', and 'bird'.Create a variable called
myPet with the type PetType and assign it the value 'cat'.Print the value of
myPet.💡 Why This Matters
🌍 Real World
Type aliases for unions help you restrict variables to specific allowed values, making your code safer and easier to understand.
💼 Career
Many jobs require writing clear and safe TypeScript code, especially when working with APIs or user input where only certain values are valid.
Progress0 / 4 steps