Enums and Modern Alternatives in TypeScript
TypeScript Enums: numeric vs string enums, const enums, bundle size pitfalls, and modern alternatives using union types and 'as const' objects.
Enums and Modern Alternatives in TypeScript
Enums allow developers to define a set of named constants. However, in modern TypeScript, plain objects with as const or string union types are widely preferred due to JavaScript emit quirks.
1. Traditional TypeScript Enums
// Numeric Enum (Auto-increments 0, 1, 2)
enum LogLevel {
Debug = 0,
Info = 1,
Error = 2,
}
// String Enum
enum UserRole {
Admin = "ADMIN",
Editor = "EDITOR",
Viewer = "VIEWER",
}2. Why Modern TypeScript Avoids Enums
- Non-Standard Runtime Code Generation: Unlike types and interfaces (which vanish completely during compilation), enums generate JavaScript IIFE objects that cannot be tree-shaken.
- Numeric Enum Insecurity: Numeric enums in older TS versions allowed assigning arbitrary out-of-bounds numbers without error.
3. The Modern Alternative: as const Objects
The industry standard pattern uses a plain JavaScript object with as const combined with a derived union type:
// 1. Plain constant object with 'as const':
export const Roles = {
Admin: "admin",
Editor: "editor",
Viewer: "viewer",
} as const;
// 2. Derive the Type automatically:
export type Role = (typeof Roles)[keyof typeof Roles];
// Role is type: "admin" | "editor" | "viewer"
// 3. Usage:
function assignRole(role: Role) {
console.log("Assigned role:", role);
}
assignRole(Roles.Admin); // ✅ Valid
assignRole("admin"); // ✅ Valid (string literal also accepted!)Related Topics
Type Narrowing & Type Guards in TypeScript
Mastering type narrowing: typeof, instanceof, in operator, custom type predicates (is), discriminated unions, and truthiness narrowing.
Modules and Type-Only Imports in TypeScript
TypeScript module architecture: import type, export type, ambient module declarations (*.d.ts), path aliases (@/*), and module resolution.