Bitlyst

Are TypeScript Enums a Good Idea? (Bundle Size & Runtime)

Are TypeScript Enums a Good Idea?

Short answer: usually no, especially in frontend apps where bundle size and runtime behavior matter.

TypeScript enums look convenient, but they come with hidden runtime costs. In most cases, there are better zero-cost alternatives.


๐Ÿง  What Actually Happens with enum

Numeric enum

enum Status {
  Idle,
  Loading,
  Success,
}

Generated JavaScript:

var Status;
(function (Status) {
  Status[Status["Idle"] = 0] = "Idle";
  Status[Status["Loading"] = 1] = "Loading";
  Status[Status["Success"] = 2] = "Success";
})(Status || (Status = {}));

๐Ÿ‘‰ This means:

  • A real object exists at runtime
  • Extra bytes in your JS bundle
  • Worse tree-shaking than you expect

String enum

enum Mode {
  Light = "light",
  Dark = "dark",
}

Still generates a runtime object:

var Mode = {
  Light: "light",
  Dark: "dark",
};

Better than numeric enums, but still not free.


โš ๏ธ Why Enums Can Be a Problem

  • โŒ Increase bundle size
  • โŒ Add runtime objects
  • โŒ Harder to tree-shake
  • โŒ Easy to misuse for simple unions

Enums are not erased by TypeScript.


โšก What About const enum?

const enum Status {
  Idle,
  Loading,
}
  • โœ… Inlined at compile time
  • โœ… Zero runtime cost
  • โŒ Often incompatible with Babel / SWC / isolatedModules
  • โŒ Risky in monorepos or mixed build setups

โš ๏ธ Use only if you fully control your toolchain.


1๏ธโƒฃ String Literal Unions (Best Default)

type Mode = "light" | "dark";
  • โœ… Zero runtime cost
  • โœ… Perfect for props, state, API types
  • โŒ No runtime list (by default)

Pair with a list if needed:

export const MODES = ["light", "dark"] as const;
export type Mode = (typeof MODES)[number];

2๏ธโƒฃ as const Object Pattern (Enum-like DX)

export const Mode = {
  Light: "light",
  Dark: "dark",
} as const;

export type Mode = typeof Mode[keyof typeof Mode];
  • โœ… Small runtime footprint
  • โœ… Tree-shakable
  • โœ… Nice DX: Mode.Dark
  • โœ… Works well with runtime validation

3๏ธโƒฃ Schema-first (APIs, forms)

const ROLES = ["admin", "user"] as const;
type Role = (typeof ROLES)[number];

Great when used with Zod / Valibot for runtime validation.


๐Ÿง  When Enums Are Actually OK

Use real enum if:

  • You need runtime iteration
  • You rely on reverse mapping
  • You integrate with legacy code
  • Bundle size is not critical

Enums are not evil โ€” just often overused.


๐Ÿ“Š Comparison Table

PatternRuntime costTree-shakingDXRecommended
enumโŒโŒโœ…โš ๏ธ
const enumโœ…โœ…โš ๏ธโš ๏ธ
union typeโœ…โœ…โœ…โญ
as const objectโœ…โœ…โœ…โญโญ

โœ… Final Recommendation

Prefer unions and as const objects in frontend TypeScript.
Reach for enum only when you truly need runtime behavior.

This approach gives you:

  • Smaller bundles
  • Faster startup
  • Clearer intent
  • Fewer build surprises

How did you like this post?

๐Ÿ‘0
โค๏ธ0
๐Ÿ”ฅ0
๐Ÿค”0
๐Ÿ˜ฎ0