What is an Enum in TypeScript?
Learn what enums are in TypeScript, how numeric and string enums differ, why regular enums exist at runtime, and when to use const enum or unions instead.
Expected Interview Answer
An enum is a TypeScript construct that defines a named set of related constant values, like `enum Direction { Up, Down, Left, Right }`, giving each member a readable name instead of a raw string or number scattered through code.
Numeric enums auto-increment from 0 unless you assign explicit values, while string enums require every member to have an explicit string value for clarity in logs and debugging. Unlike interfaces and type aliases, a regular enum is NOT erased at compile time — it emits an actual JavaScript object at runtime, which is why enums have a real runtime footprint, unlike most other TypeScript type constructs. `const enum` is a variant that IS inlined and erased, avoiding that runtime object, but it cannot be used across certain module boundaries. Many teams now prefer union-of-string-literals (`type Direction = 'up' | 'down'`) since it is zero-cost and structurally compatible.
- Groups related named constants under one readable identifier
- String enums are self-documenting in debugger output and logs
- Prevents typos versus scattering raw string literals everywhere
- `const enum` inlines values with zero runtime object overhead
- Works well with switch statements for exhaustiveness checking
AI Mentor Explanation
An enum is like the official list of dismissal types a scorer chooses from — bowled, caught, run out — rather than writing free-text notes that could be misspelled. Just as the scorebook actually stores that fixed vocabulary as real entries on the sheet, a regular TypeScript enum genuinely exists as an object at runtime, not just a rule in the scorer's head.
Step-by-Step Explanation
Step 1
Define the enum
Write `enum Direction { Up, Down, Left, Right }` to create a named group of related constants.
Step 2
Numeric auto-increment
Without explicit values, members get 0, 1, 2, 3 in declaration order; you can override with `Up = 1` and later members continue from there.
Step 3
String enums
`enum Status { Active = 'ACTIVE', Inactive = 'INACTIVE' }` requires every member to have an explicit string, improving log readability.
Step 4
Runtime object
A regular enum compiles into an actual JavaScript object with forward and reverse (numeric) lookups, unlike interfaces which vanish entirely.
Step 5
Consider const enum or unions
Use `const enum` to inline values with no runtime object, or a string-literal union type for a zero-cost, fully erased alternative.
What Interviewer Expects
- Knows enums, unlike interfaces/types, produce real runtime JavaScript
- Can distinguish numeric enums from string enums
- Understands `const enum` inlines and avoids the runtime object
- Can explain when a string-literal union might be preferred instead
- Mentions reverse mapping behavior for numeric enums
Common Mistakes
- Assuming all enums are erased at compile time like interfaces
- Mixing numeric and string values in the same enum without realizing it disallows auto-increment
- Not knowing numeric enums generate a reverse-lookup mapping
- Overusing enums where a simpler string union type would be lighter weight
Best Answer (HR Friendly)
“An enum is a way to define a fixed, named set of options in TypeScript, like a list of valid statuses or directions, so the code stays readable and avoids typos. Unlike some other TypeScript features, enums actually produce real code that exists when the program runs, not just type-checking rules.”
Code Example
enum Direction {
Up, // 0
Down, // 1
Left, // 2
Right, // 3
}
enum Status {
Active = 'ACTIVE',
Inactive = 'INACTIVE',
}
console.log(Direction.Up); // 0 — real runtime value
console.log(Direction[0]); // 'Up' — reverse mapping exists for numeric enumsconst enum Level { Low, Medium, High }
const l = Level.Medium; // inlined to `1` at compile time, no Level object emittedFollow-up Questions
- Why do numeric enums have reverse mappings but string enums do not?
- What is the difference between `enum` and `const enum` at compile time?
- When would you prefer a string-literal union over an enum?
- Can you mix numeric and string values in the same enum?
- How do enums interact with exhaustiveness checks in a switch statement?
MCQ Practice
1. What is the value of `Direction.Down` given `enum Direction { Up, Down, Left, Right }`?
Numeric enums auto-increment starting at 0, so Up=0, Down=1, Left=2, Right=3.
2. Which statement about `const enum` is correct?
`const enum` members are inlined directly at their usage sites, so no enum object is emitted in the compiled JavaScript.
3. Unlike interfaces and type aliases, what is true of a regular TypeScript enum?
A regular enum is one of the few TypeScript constructs that generates actual runtime JavaScript, unlike types and interfaces which are erased.
Flash Cards
Does a regular enum exist at runtime? — Yes — it compiles into a real JavaScript object, unlike interfaces and type aliases which are erased.
What values do string enum members require? — Every member must have an explicit string value; there is no auto-increment for string enums.
What does `const enum` do differently? — It inlines member values directly at usage sites and emits no runtime object at all.
What do numeric enums generate that string enums do not? — A reverse mapping, so `Direction[0]` returns `'Up'` in addition to `Direction.Up` returning `0`.