What are enums in TypeScript?

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.