> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# TypeScript Const Enum

A `const enum` is a completely erased, compile-time-only enumeration in TypeScript. Unlike standard enums, which generate a reverse-mapped object via an Immediately Invoked Function Expression (IIFE) in the emitted JavaScript, a `const enum` leaves no runtime footprint. Instead, the TypeScript compiler directly inlines the evaluated enum member values at every call site during the emit phase.

```typescript theme={"dark"}
const enum Direction {
  Up = 1,
  Down = 2,
  Left = 3,
  Right = 4
}
```

## Compilation Behavior

The defining characteristic of a `const enum` is its compilation output. The compiler substitutes the property access with the literal value.

**TypeScript Source:**

```typescript theme={"dark"}
const enum HttpStatusCode {
  OK = 200,
  NotFound = 404
}

const responseCode = HttpStatusCode.OK;
```

**Emitted JavaScript:**

```javascript theme={"dark"}
const responseCode = 200 /* HttpStatusCode.OK */;
```

*Note: The compiler appends an inline comment indicating the original enum member for debugging purposes.*

## Technical Constraints and Characteristics

* **No Runtime Object:** Because the enum is erased, you cannot iterate over a `const enum` using `Object.keys()`, `Object.values()`, or `for...in` loops. The identifier `HttpStatusCode` does not exist at runtime.
* **Constant Expressions Only:** Members of a `const enum` must be initialized with constant expressions. They cannot be initialized with dynamically computed values (e.g., the result of a function call or a property access from a runtime object).

```typescript theme={"dark"}
// Valid: Constant expressions
const enum BitFlags {
  None = 0,
  Read = 1 << 0,
  Write = 1 << 1
}

// Invalid: Computed value
const enum Dynamic {
  Id = Math.random() // Error: const enum member initializers must be constant expressions
}
```

* **Reverse Mapping:** Unlike standard numeric enums, numeric `const enums` do not support reverse mapping (accessing the string key via the numeric value), as the mapping object is never generated.

## Compiler Configuration Impacts

The behavior of `const enum` interacts specifically with certain `tsconfig.json` compiler options and third-party transpilation tools:

* **`preserveConstEnums`**: If set to `true`, the TypeScript compiler will still inline the values at the call sites, but it will *also* emit the standard enum object into the JavaScript output.
* **`isolatedModules`**: This is strictly a type-checking flag that warns developers about constructs that would be unsafe for single-file transpilers. It does not change `tsc`'s own compilation process, nor does it prevent `tsc` from performing full-program analysis and inlining. Exporting a regular `const enum` and importing it into another file is perfectly valid under `isolatedModules`. TypeScript will only flag *ambient* const enums (`export declare const enum`) under this setting, as those lack implementations for single-file transpilers to emit.
* **Single-File Transpilers (Babel, esbuild, SWC)**: Because modern bundlers and transpilers operate on a strictly per-file basis, they cannot perform the cross-file analysis required to inline values from an imported `const enum`. To handle this, these tools intentionally compile `const enum` declarations into standard enums (emitting the runtime object). This ensures that imported values resolve correctly at runtime and prevents `ReferenceError`s.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor TypeScript Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
