What are Enums in TypeScript?
Learn TypeScript enums — numeric, string and const enums, reverse mapping, benefits, code examples and common interview questions with clear answers.
Expected Interview Answer
An enum in TypeScript is a named set of related constant values, letting you refer to a fixed group of options by descriptive names instead of raw numbers or strings.
Enums come in numeric (auto-incrementing from 0 by default), string, and heterogeneous forms. Numeric enums also generate a reverse mapping from value back to name. A const enum is fully inlined at compile time and emits no runtime object, while a regular enum compiles to a JavaScript object you can iterate. Enums improve readability and centralize a set of allowed values in one place.
- Replaces magic numbers and loose strings with named constants
- Groups related options under one type
- Numeric enums provide reverse value-to-name lookup
- const enums inline values for zero runtime cost
- Improves autocomplete and refactoring safety
AI Mentor Explanation
An enum is like the fixed set of dismissal types in cricket — Bowled, Caught, LBW, RunOut, Stumped. Instead of scribbling a random code number for how a batter got out, the scorer picks one named entry from a settled list, so every scorecard uses the same clear, agreed vocabulary.
Step-by-Step Explanation
Step 1
Declare the enum
Use the enum keyword followed by a name and a block of members, e.g. enum Direction { Up, Down }.
Step 2
Understand default numbering
Numeric enum members auto-increment from 0 unless you assign explicit values.
Step 3
Assign explicit or string values
Set members like Status = 'ACTIVE' for string enums to control the emitted value.
Step 4
Access members
Reference values with dot notation, e.g. Direction.Up, and rely on autocomplete for valid options.
Step 5
Choose const when possible
Use const enum to inline values at compile time when you don't need a runtime object or iteration.
What Interviewer Expects
- Knowing the difference between numeric and string enums
- Awareness of reverse mapping in numeric enums
- Understanding what const enum does at compile time
- When to prefer union types over enums
- A clear code example
Common Mistakes
- Assuming string enums have reverse mapping (they don't)
- Forgetting numeric enums generate a runtime object
- Confusing const enum inlining with regular enum behaviour
- Using enums where a simple string union would be lighter
Best Answer (HR Friendly)
“An enum is a way to give friendly names to a fixed set of related options, like the difficulty levels in a game. Instead of using confusing numbers or loose text, the code uses clear labels, which makes it easier to read and harder to make mistakes.”
Code Example
enum Direction {
Up, // 0
Down, // 1
Left, // 2
Right, // 3
}
enum Status {
Active = 'ACTIVE',
Inactive = 'INACTIVE',
}
const move: Direction = Direction.Up;
console.log(Direction[0]); // 'Up' (reverse mapping)
console.log(Status.Active); // 'ACTIVE'Follow-up Questions
- What is the difference between a numeric enum and a string enum?
- What does const enum do and when should you use it?
- When would you choose a union type over an enum?
- Why do numeric enums have reverse mapping but string enums do not?
- How do you iterate over the members of an enum?
MCQ Practice
1. What is the default value of the first member in a numeric enum?
Numeric enum members auto-increment starting at 0 unless an explicit value is assigned.
2. Which enum kind provides a reverse mapping from value to name?
Numeric enums generate both name-to-value and value-to-name entries; string enums do not.
3. What is the main effect of declaring a const enum?
A const enum is erased and its member values are inlined, emitting no runtime object.
Flash Cards
What is a TypeScript enum? — A named group of related constant values referenced by descriptive names.
Default numeric enum start value? — 0, auto-incrementing for each subsequent member.
Do string enums have reverse mapping? — No — only numeric enums generate value-to-name lookups.
What does const enum do? — Inlines member values at compile time and emits no runtime object.