What if you could avoid confusing bugs caused by mistyped values with just one simple tool?
Why Enum member access in Typescript? - Purpose & Use Cases
Imagine you have a list of colors and you want to use their exact names and values throughout your code. Without enums, you might write each color as a separate constant or string everywhere.
This manual way is slow and risky. You can easily mistype a color name or value, causing bugs that are hard to find. Changing a color value means hunting down every place it appears.
Enums let you group related values under one name. You access members by their names, which keeps your code clean, consistent, and easy to update in one place.
const RED = 1; const GREEN = 2; const BLUE = 3; function paint(color) { if (color === 1) { /* red paint */ } }
enum Color {
Red = 1,
Green,
Blue
}
function paint(color: Color) {
if (color === Color.Red) { /* red paint */ }
}You can write safer, clearer code by referring to meaningful names instead of magic numbers or strings everywhere.
In a game, you can define player states like Idle, Running, and Jumping as enum members, making it easy to check and update the player's current state without errors.
Enums group related values under one name.
Accessing enum members by name avoids mistakes and magic numbers.
Updating values is simple and safe in one place.
Practice
Which of the following is the correct way to access a member named Red from an enum called Color?
Solution
Step 1: Understand enum member access syntax
In TypeScript, enum members are accessed using dot notation with the enum name first, then the member name.Step 2: Apply correct syntax to the example
For enumColorand memberRed, the correct access isColor.Red.Final Answer:
Color.Red -> Option AQuick Check:
Enum member access = Color.Red [OK]
- Reversing the order like Red.Color
- Using brackets without quotes for string enums
- Calling enum members as functions
Which of the following is the correct syntax to define a numeric enum named Direction with members Up, Down, Left, and Right?
?
Solution
Step 1: Recall enum declaration syntax
In TypeScript, enums are declared using the keywordenumfollowed by the name and curly braces containing members.Step 2: Match correct syntax
enum Direction { Up, Down, Left, Right } usesenum Direction { Up, Down, Left, Right }, which is the correct syntax.Final Answer:
enum Direction { Up, Down, Left, Right } -> Option CQuick Check:
Enum declaration uses curly braces {} [OK]
- Using equals sign (=) after enum name
- Using colon (:) instead of braces
- Using parentheses () instead of braces
Given the enum:
enum Status { Active = 1, Inactive, Pending }What is the output of console.log(Status[2])?
Solution
Step 1: Understand numeric enum reverse mapping
Numeric enums in TypeScript create a reverse mapping from value to name. Here,Active = 1, soInactiveis 2,Pendingis 3.Step 2: Access enum member by value
Status[2]returns the name of the member with value 2, which isInactive.Final Answer:
"Inactive" -> Option BQuick Check:
Numeric enum reverse lookup = "Inactive" [OK]
- Assuming string enums have reverse mapping
- Confusing member names with values
- Expecting numeric output instead of string
Consider this enum and code snippet:
enum Fruit { Apple = "APPLE", Banana = "BANANA" }
console.log(Fruit["APPLE"]);What will happen when this code runs?
Solution
Step 1: Understand string enum member access
String enums do not have reverse mapping from value to name like numeric enums.Step 2: Analyze the code behavior
AccessingFruit["APPLE"]looks for a member named "APPLE", but enum keys are "Apple" and "Banana". So it returnsundefined.Final Answer:
undefined -> Option AQuick Check:
String enums lack reverse mapping = undefined [OK]
- Expecting reverse mapping for string enums
- Confusing enum keys with values
- Assuming case-insensitive access
You have this enum and function:
enum Status { Success = 200, NotFound = 404, ServerError = 500 }
function getStatusName(code: number): string {
return Status[code] || "Unknown";
}What will getStatusName(404) return, and why?
Solution
Step 1: Recognize numeric enum reverse mapping
Numeric enums in TypeScript create a reverse mapping from numeric values to member names.Step 2: Evaluate function behavior with input 404
CallinggetStatusName(404)returnsStatus[404], which is "NotFound". If no match, it returns "Unknown".Final Answer:
"NotFound" because numeric enums support reverse mapping -> Option DQuick Check:
Numeric enum reverse lookup returns member name [OK]
- Thinking it returns the number as string
- Assuming reverse mapping doesn't exist
- Expecting runtime errors on valid enum access
Ready to prove it?
Pass the test and this pattern moves unknown
