1. Introduction
An enum (short for enumeration) lets you define a named set of related constant values, making code more readable than scattering magic numbers or strings throughout a codebase. TypeScript supports numeric enums, string enums, and a more efficient variant called const enums.
Cricket analogy: Instead of writing the magic number 4 for a boundary all over your scorer app, a MatchResult enum with Won, Lost, Tied, Draw reads clearly, the same benefit TypeScript enums give over scattered constants.
Enums are one of the few TypeScript features that are not purely a compile-time construct — by default, they generate real JavaScript code that exists at runtime.
Cricket analogy: A printed team sheet exists physically at the ground, not just on paper in someone's head; likewise, unlike most TypeScript features that vanish at compile time, enums generate real JavaScript objects that exist at runtime.
2. Syntax
enum Direction {
Up,
Down,
Left,
Right,
}
enum Status {
Active = "ACTIVE",
Inactive = "INACTIVE",
}
const enum FastDirection {
Up,
Down,
}3. Explanation
Numeric enums auto-increment starting at 0 unless you assign explicit values. String enums require every member to have an explicit string value and offer clearer runtime debugging output than numeric enums.
Cricket analogy: Batting positions numbered 1 through 11 auto-increment from the top of the order unless a captain reassigns one; similarly numeric enums auto-increment from 0 unless you assign explicit values, while string enums like "OPENER" need every value spelled out.
Enums are often compared to a union of string literal types (e.g. type Status = "ACTIVE" | "INACTIVE"), which achieves similar compile-time safety with a lighter footprint.
Cricket analogy: A MatchStatus enum with Live, Completed, Abandoned behaves much like the union type "LIVE" | "COMPLETED" | "ABANDONED" — both give compile-time safety, but the literal union skips generating any runtime object at all.
TypeScript numeric and string enums compile to a real JavaScript object at runtime — they are not erased like most type constructs. Numeric enums additionally generate a reverse mapping, so Direction[0] returns "Up" in addition to Direction.Up returning 0. This is fundamentally different from a union-of-literals type like "Up" | "Down", which produces zero runtime code and is fully erased during compilation — there is no JavaScript object to inspect for a union type.
A const enum is fully erased at compile time: every usage is inlined as its literal value, and no enum object is emitted at all. This makes const enums more efficient, but they cannot be used with certain build tools (like isolated file transpilation in Babel) that compile files independently without full program knowledge, since inlining requires cross-file type information.
4. Example
enum Direction {
Up,
Down,
Left,
Right,
}
function move(dir: Direction): string {
return `Moving ${Direction[dir]}`;
}
console.log(move(Direction.Up));
console.log(Direction.Up);
console.log(Direction[0]);
console.log(typeof Direction);5. Output
Moving Up
0
Up
object6. Key Takeaways
- Enums define a named group of related constants — numeric, string, or const.
- Numeric enums auto-increment from 0 by default and generate a reverse mapping.
- String enums require explicit values for every member and have no reverse mapping.
- Regular enums compile to a real JavaScript object that exists at runtime.
- A union of string literals is fully erased at compile time — zero runtime footprint, unlike a regular enum.
- const enum members are inlined at every usage site and generate no object at all.
Practice what you learned
1. What value does the first member of `enum Direction { Up, Down }` get by default?
2. What is unique about how regular TypeScript enums are compiled compared to most other type constructs like interfaces?
3. For `enum Direction { Up, Down }`, what does `Direction[0]` evaluate to at runtime?
4. How does a union of string literal types like `"ACTIVE" | "INACTIVE"` differ from a string enum at runtime?
5. What makes const enums different from regular enums?
Was this page helpful?
You May Also Like
Literal Types in TypeScript
Narrow a type down to one exact value -- a specific string, number, or boolean -- and combine literals into precise unions.
Union and Intersection Types in TypeScript
Combine types with `|` (union) and `&` (intersection) to model values that can be one of several shapes or must satisfy all of them.
Type Aliases in TypeScript
Use `type` to give a name to any type -- primitives, unions, tuples, function signatures, and more.
any, unknown, never and void in TypeScript
The four special types that sit outside normal type checking — the unsafe escape hatch, the safe escape hatch, the impossible type, and the absent-return type.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics