# Enums vs Const Objects

> Why modern TypeScript developers are abandoning enums.

Subject: TypeScript | Lesson 7 of 54 | 4 min read
Interactive lesson: https://codewithanimation.in/typescript/enums-vs-const-objects
Previous: [Objects & Interfaces](https://codewithanimation.in/typescript/object-types-interfaces.md) | Next: [Nullish Values & !](https://codewithanimation.in/typescript/nullish-values.md)

## The Enum debate

An `enum` is a feature that allows you to define a set of named constants. However, unlike most TypeScript features which disappear completely when compiled, `enum` actually generates real JavaScript code at runtime.

A modern alternative is to use a plain JavaScript object paired with an `as const` assertion. This locks the object's values so they can't be changed, providing the same type-safety as an enum but with much cleaner output.

## The bundle size tax

Because `enum` is not a native JavaScript feature (yet), the TypeScript compiler has to transform it into a complex Immediately Invoked Function Expression (IIFE).

This adds unnecessary bulk to your JavaScript bundle. Worse, bundlers like Webpack and Rollup sometimes struggle to 'tree-shake' (remove unused parts) of enums because of how the IIFE is structured.

## The modern alternative

Instead of writing this:

```typescript
enum Status {
  Pending = "PENDING",
  Active = "ACTIVE"
}
```

Modern codebases write this:

```typescript
const Status = {
  Pending: "PENDING",
  Active: "ACTIVE"
} as const;

// Extract the union type of its values:
type StatusType = typeof Status[keyof typeof Status];
```

## Common mistake: Numeric Enum weirdness

If you don't assign string values to an enum, TypeScript defaults to numeric values starting at 0. But it also creates a 'reverse mapping'.

This means `enum Direction { Up }` results in an object where `Direction.Up === 0`, but also `Direction[0] === "Up"`. This behavior is highly unexpected for most developers and causes confusing bugs when iterating over keys.

## Tip: String Unions are often enough

Before creating an object with `as const`, ask yourself if you even need an object at runtime. Often, a simple string union (`type Status = 'PENDING' | 'ACTIVE'`) is exactly what you need, and it compiles to zero bytes of JavaScript.

## Try it: Try it

Watch the TypeScript compiler generate JavaScript for both an `enum` and a `const` object. Notice the massive difference in output size.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/enums-vs-const-objects

## Check yourself

### Question 1

Why do some bundlers struggle to optimize (tree-shake) TypeScript enums?

- A. Because enums are erased during compilation.
- B. Because enums compile into complex IIFEs (Immediately Invoked Function Expressions).
- C. Because enums require experimental decorators to work.

Correct answer: B

The IIFE pattern used to generate enums at runtime is notoriously difficult for static analysis tools to safely tree-shake.

- Why not A: Enums are one of the few TS features that are NOT erased.
- Why not C: Enums are a core TS feature, not a decorator feature.

## Remember this

- Enums leave a footprint in your compiled JavaScript bundle.
- Numeric enums create confusing reverse-mappings.
- Use `as const` objects for runtime constants.
- Use string unions (`type A = 'X' | 'Y'`) when you don't need runtime access.
