What are enums in TypeScript?

Why Interviewers Ask This

This question tests conceptual clarity. Interviewers want to hear a precise, confident definition before moving to more complex TypeScript topics. It also reveals how well you can explain technical ideas to non-experts.

Answer

Enums let you define a set of named constants. TypeScript supports numeric and string enums. Numeric enum: enum Direction { North, South, East, West } — values are 0, 1, 2, 3 by default (auto-incremented). Access: Direction.North === 0. You can set a custom starting value: enum Status { Active = 1, Inactive, Archived }. String enum: enum Color { Red = "RED", Green = "GREEN", Blue = "BLUE" } — more readable, preferred for serialized values and debugging. Const enum: const enum Size { Small, Medium, Large } — completely inlined at compile time, no runtime object generated, better performance. Caveats: numeric enums allow reverse lookup (Direction[0] === "North"), which can be surprising. Many TypeScript experts prefer using string literal union types (type Direction = "North" | "South") over enums for their simplicity and better tree-shaking.

Common Mistake

Many candidates answer correctly but can't explain the 'why'. Always be prepared to justify your answer with a concrete example or use case from your TypeScript experience.