Reverse mapping in numeric enums
📖 Scenario: Imagine you are creating a simple traffic light system in TypeScript. Each light color has a number code. You want to be able to find the color name from the number code and also get the number code from the color name.
🎯 Goal: Build a numeric enum for traffic light colors and use reverse mapping to find the color name from a number code.
📋 What You'll Learn
Create a numeric enum called
TrafficLight with exact members: Red = 1, Yellow = 2, Green = 3Create a variable called
lightNumber and set it to 2Use reverse mapping to get the color name from
lightNumber and store it in a variable called lightNamePrint the value of
lightName to the console💡 Why This Matters
🌍 Real World
Enums with reverse mapping are useful in programs where you need to convert between codes and names, like traffic lights, status codes, or menu options.
💼 Career
Understanding enums and reverse mapping helps in writing clear and maintainable TypeScript code, a skill valuable for frontend and backend developers working with typed JavaScript.
Progress0 / 4 steps