Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. TypeScript Dictionary
  3. enum

enum

Since: TypeScript 1.0(2014)

A type that groups related constants together. By giving meaningful names to numeric or string values, you improve the readability and maintainability of your code.

Syntax

enum EnumName {
	Member1, // 0
	Member2, // 1
	Member3, // 2
}

// String enum: each member is explicitly assigned a string value.
enum EnumName {
	Member1 = "value1",
	Member2 = "value2",
}

// const enum: an optimized version that is inlined at compile time.
const enum EnumName {
	Member1,
	Member2,
}

Syntax overview

SyntaxDescription
enum Name { ... }Defines an enumeration. Defaults to a numeric enum, assigning values starting from 0.
Numeric enumAssigns numeric values to members. Supports both auto-incrementing and custom starting values.
String enumAssigns string values to members. Recommended for real-world use because values appear as readable strings during debugging.
const enumProduces no JavaScript output after compilation — values are inlined directly at each usage site. This offers a performance advantage.

Sample code

enum Direction {
	Up, // 0
	Down, // 1
	Left, // 2
	Right, // 3
}
let move: Direction = Direction.Up;
console.log(move); // Outputs: 0
console.log(Direction[0]); // Outputs: "Up" (reverse mapping is supported).

// You can also specify a custom starting value.
enum HttpStatus {
	OK = 200,
	NotFound = 404,
	InternalServerError = 500,
}
console.log(HttpStatus.OK); // Outputs: 200

// String enum: useful because values appear as readable strings in debug output.
enum Color {
	Red = "RED",
	Green = "GREEN",
	Blue = "BLUE",
}
let favorite: Color = Color.Green;
console.log(favorite); // Outputs: "GREEN"

// Using an enum in a switch statement makes the intent clear.
function getLabel(status: HttpStatus): string {
	switch (status) {
		case HttpStatus.OK: return "Success";
		case HttpStatus.NotFound: return "Not Found";
		case HttpStatus.InternalServerError: return "Server Error";
	}
}
console.log(getLabel(HttpStatus.NotFound)); // Outputs: "Not Found"

// const enum: values are inlined directly after compilation.
const enum Size {
	Small = "S",
	Medium = "M",
	Large = "L",
}
let tshirt: Size = Size.Medium; // After compilation: let tshirt = "M";
console.log(tshirt); // Outputs: "M"

Running the above produces the following output:

npx ts-node ts_enum.ts
0
Up
200
GREEN
Not Found
M

Overview

`enum` lets you group related constant values under a single name and access them with readable labels. For example, representing game direction keys as `Direction.Up` and `Direction.Down` makes the intent far clearer than using raw numbers like `0, 1, 2, 3`.

Numeric enums support reverse mapping — you can retrieve `"Up"` from `Direction[0]` — but string enums do not. However, string enums have the advantage that their values appear as meaningful strings in debug output, which is why they are often preferred in practice.

Note: enum is a TypeScript-specific feature and compiles to a JavaScript object. If bundle size is a concern, you may prefer an object literal with `as const` instead (see Literal Types).

If you find any errors or copyright issues, please .