enum Enum Type
Last updated: 2026-10-22
enum Enum Type
Enums group related constants under one namespace, where members are both types and values. Numeric enums auto-increment from 0; string enums take explicit values and read better.
| Group | Basic Types |
|---|---|
| Kind | Type |
| Strict mode | Not required |
ℹ️ Info The editor transpiles TypeScript to JavaScript and runs it (no type checking); see each example's Output for type-check results.
📝 Syntax
enum Direction { Up, Down } // numeric enum: 0, 1
enum Level { Info = "info", Error = "error" } // string enum
⚙️ Parameters
| Member = 0 | Numeric members auto-increment from 0 (assignable); string members must be assigned explicitly |
|---|---|
| Enum[key] | Only numeric enums support reverse lookup Direction[0]; string enums do not |
Returns:Enums emit a real runtime object — unlike interface/type, they are not pure types.
▶ Example
Edit the code below and click Try it to see the transpiled result:
Output:
1 Shipped
warn
(Runtime output after transpilation; Paid has index 1 and OrderStatus[2] reverse-maps to Shipped)
💡 Notes
ℹ️ Info Numeric enums have a reverse map (OrderStatus[1] → "Paid"); string enums do not.
💡 Tip String enums stay meaningful in logs and serialized data ("warn" not 1) — prefer them for public APIs.
⚠️ Note Enums overlap with literal unions; many teams prefer the latter (no runtime code). Start with literal unions in new projects and reach for enums only when needed.
🏷️ Related Basic Types
💡 Related cases
More cases coming soon.
📚 Related tutorials
❓ FAQ
Q enum or literal union type?
A Both restrict values. Enums are real objects usable at runtime; literal unions cost nothing at runtime and interoperate with plain strings. Literal unions win in most cases.
Q How does numeric enum numbering work by default?
A Auto-increment starts at 0; after an explicit value, following members continue from it: { A, B = 5, C } yields 0, 5, 6.
Q What is const enum?
A const enum inlines member references at compile time without emitting the enum object. It has issues with isolated compilation (Babel/swc) — prefer as const objects in modern setups.