Suggest an editImprove this articleRefine the answer for “What is enum in TypeScript?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**`enum`** (enumeration) is a special TypeScript type that defines a set of named constants for a fixed set of possible values. **Key point:** an enum can be numeric, string, heterogeneous, or a `const enum`, and only numeric enums support reverse mapping.Shown above the full answer for quick recall.Answer (EN)Image### What `enum` is in TypeScript `enum` **(enumeration)** is a special type in TypeScript that lets you define a **set of named constants**. It is used when you need to represent a **fixed set of possible values**, for example roles, statuses, directions, and so on. --- ### Why `enum` is needed Instead of writing "magic strings" or numbers by hand: ```javascript if (status === "loading") { ... } ``` you can use **named values**, which: - makes the code **more readable**, - reduces the risk of **typos**, - improves **autocomplete** and **type checking**. --- ### How to declare an `enum` #### Example 1. Numeric `enum` (default) ```javascript enum Direction { Up, Down, Left, Right, } let move: Direction = Direction.Up; console.log(move); // 0 ``` > By default TypeScript assigns values starting from `0` (`Up = 0, Down = 1, ...`). --- #### Example 2. Setting your own numeric values ```javascript enum StatusCode { OK = 200, NotFound = 404, ServerError = 500, } console.log(StatusCode.OK); // 200 console.log(StatusCode[404]); // "NotFound" ``` > Numeric enums are two-way: you can get the **name from the value**, and the **value from the name**. --- #### Example 3. String `enum` ```javascript enum Direction { Up = "UP", Down = "DOWN", Left = "LEFT", Right = "RIGHT", } console.log(Direction.Left); // "LEFT" ``` > Unlike numeric ones, **string enums are one-way**: > you cannot get the key from the value. --- #### Example 4. Heterogeneous (mixed) `enum` (not recommended) ```javascript enum Mix { Yes = 1, No = "NO", } ``` > This variant exists, but it **hurts readability** and gets in the way of autocomplete. > It is better to avoid mixed types. --- ### How to use `enum` #### In variables: ```javascript let currentDirection: Direction = Direction.Right; ``` #### In functions: ```javascript function movePlayer(direction: Direction) { switch (direction) { case Direction.Up: console.log("Going up"); break; case Direction.Down: console.log("Going down"); break; } } ``` #### In interfaces: ```javascript enum UserRole { Admin, User, Guest, } interface User { name: string; role: UserRole; } const tim: User = { name: "Tim", role: UserRole.Admin, }; ``` --- ### Automatic numbering TypeScript numbers the values itself if you did not specify them manually: ```javascript enum Priority { Low, // 0 Medium, // 1 High, // 2 } ``` You can set a starting value - the rest will follow in order: ```javascript enum Priority { Low = 1, // 1 Medium, // 2 High, // 3 } ``` --- ### Compilation to JavaScript At compile time, TypeScript turns an `enum` into a regular object: ```javascript var Direction; (function (Direction) { Direction[(Direction["Up"] = 0)] = "Up"; Direction[(Direction["Down"] = 1)] = "Down"; })(Direction || (Direction = {})); ``` > This allows reverse mapping (numeric enums are two-way). --- ### Alternative: `const enum` If you do not need two-way mapping and want **maximum performance**, use `const enum`: it is **removed at compile time** and does not create an object in the JS code: ```javascript const enum Direction { Up, Down, Left, Right, } const dir = Direction.Up; console.log(dir); // 0 ``` It simply compiles to: ```javascript const dir = 0 /* Up */; ``` --- ### Summary | Enum kind | Example | Values | Reverse mapping | |---|---|---|---| | Numeric | `enum A { X, Y }` | 0, 1, ... | Yes | | String | `enum A { X = "x" }` | "x" | No | | Heterogeneous | `enum A { X = 1, Y = "y" }` | 1, "y" | Not recommended | | `const enum` | `const enum A { X, Y }` | inlined into the code | No (inline) |For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.