TypeScript Enums Cheat Sheet
Covers numeric and string enums, const enums, reverse mapping, and modern alternatives like literal unions and the as const pattern.
1 PageIntermediateMar 22, 2026
Numeric & String Enums
The two core enum flavors and how their values differ.
typescript
enum Direction { Up, Down, Left, Right } // numeric: 0, 1, 2, 3console.log(Direction.Up); // 0console.log(Direction[0]); // "Up" - reverse mapping (numeric enums only)enum Status { // custom numeric start Active = 1, Inactive, // 2 (auto-increments) Pending, // 3}enum Role { // string enum - no reverse mapping, more debuggable Admin = 'ADMIN', User = 'USER', Guest = 'GUEST',}console.log(Role.Admin); // "ADMIN"
Const Enums
Fully inlined enums that avoid emitting a runtime object.
typescript
const enum Direction2 { Up, Down, Left, Right }let d = Direction2.Up; // inlined to `let d = 0;` at compile time, no object emitted// Cannot use computed members or reverse mapping with const enum// Cannot be used with isolatedModules (each file compiled independently)
Union Types & 'as const' as Alternatives
Common patterns teams use instead of enums.
typescript
// Many teams prefer literal union types over enumstype Role2 = 'ADMIN' | 'USER' | 'GUEST';// 'as const' object pattern - good tree-shaking, no separate enum typeconst Role3 = { Admin: 'ADMIN', User: 'USER', Guest: 'GUEST',} as const;type Role3Type = typeof Role3[keyof typeof Role3]; // 'ADMIN' | 'USER' | 'GUEST'
Key Facts
Behaviors worth knowing before choosing an enum style.
- Numeric enum reverse mapping- Numeric enums generate both Name-to-value and value-to-Name lookups on the compiled object
- String enums- No reverse mapping; each member must be initialized explicitly
- Heterogeneous enums- Mixing string and numeric members is allowed but discouraged
- const enum- Fully inlined at compile time; produces no runtime object, smaller bundle
- Computed members- Numeric enum members can use expressions, but only the first member can be uninitialized after one
- enum as a type- The enum name doubles as a type: function f(r: Role) {}
Pro Tip
Prefer string enums or an 'as const' object over numeric enums for anything serialized (API payloads, database values, logs) - numeric enum values are position-dependent, so inserting a new member in the middle silently shifts every downstream integer value.
Was this cheat sheet helpful?
Explore Topics
#TypeScriptEnums#TypeScriptEnumsCheatSheet#Programming#Intermediate#NumericStringEnums#ConstEnums#Union#Types#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance