Enum member access in Typescript - Time & Space Complexity
Let's see how fast we can get values from an enum in TypeScript.
We want to know how the time to access enum members changes as the enum grows.
Analyze the time complexity of the following code snippet.
enum Colors {
Red,
Green,
Blue,
Yellow,
Purple
}
const colorName = Colors[2];
This code gets the name of the color with the number 2 from the enum.
- Primary operation: Direct lookup of enum member by key.
- How many times: Only once per access, no loops or repeated steps.
Getting a value from an enum is like looking up a word in a dictionary.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time to access an enum member stays the same no matter how many members there are.
Time Complexity: O(1)
This means accessing an enum member takes the same short time, even if the enum is very big.
[X] Wrong: "Accessing an enum member takes longer if the enum has more members."
[OK] Correct: Enum members are stored like keys in a map, so access is direct and fast regardless of size.
Knowing that enum access is quick helps you explain how your code handles fixed sets of values efficiently.
"What if we used a function to search for a value inside an enum instead of direct access? How would the time complexity change?"
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
