Enums look elegant, but they secretly bloat your JavaScript output. Each one compiles into a function wrapper that adds unnecessary weight to your bundle.
Example:
// Old way with enums enum PaymentMethod { Credit = 'credit', Debit = 'debit', Cash = 'cash', }Usage:
const method = PaymentMethod.Credit;After compilation:
var PaymentMethod; (function (PaymentMethod) { PaymentMethod["Credit"] = "credit"; PaymentMethod["Debit"] = "debit"; PaymentMethod["Cash"] = "cash"; })(PaymentMethod || (PaymentMethod = {}));Each key creates more boilerplate. In large projects with dozens of enums, this overhead adds up.
Replace enums with const objects and a type alias for keys.
// Modern way const PaymentMethod = { Credit: 'credit', Debit: 'debit', Cash: 'cash', } as const; type PaymentMethod = keyof typeof PaymentMethod; const method: PaymentMethod = PaymentMethod.Credit;✅ Still type-safe
✅ Works with IntelliSense
✅ Generates minimal JavaScript
If you need reverse mapping, write a helper:
function findKey<T extends Record<string, unknown>>( obj: T, value: T[keyof T] ): string | undefined { return Object.keys(obj).find((k) => obj[k] === value); } const found = findKey(HttpResponse, 401); // "NotAuthorized"Before: 1.1MB bundle, 20 enums
After: 880KB bundle, 0 enums
That’s a 20% reduction with no loss of developer experience.
Enums aren’t always bad, but in most cases, const objects with type aliases:
Keep type safety
Preserve autocomplete
Generate cleaner JavaScript
Reduce bundle size
If you don’t need reverse mapping, ditch enums.