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.

GroupBasic Types
KindType
Strict modeNot 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 = 0Numeric 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.
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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏