100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
TypeScript

Enums in TypeScript

Named sets of constants — numeric, string, and const enums — and how they differ from plain unions of literal types at runtime.

Basic TypesIntermediate9 min readJul 8, 2026
Analogies

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

typescript
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

typescript
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

text
Moving Up
0
Up
object

6. 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

Was this page helpful?

Topics covered

#TypeScript#TypeScriptProgrammingStudyNotes#Programming#EnumsInTypeScript#Enums#Syntax#Explanation#Example#StudyNotes#SkillVeris