TypeScript Enum Generator
指导
TypeScript Enum Generator
Generate TypeScript enums, const objects, and union types from a simple list of values. Paste your values, choose a naming style and output format, and get ready-to-use TypeScript code with optional JSDoc comments, type guards, reverse mappings, and value arrays.
如何使用
Enter your values one per line or comma-separated into the input area. Set your enum name, choose a key naming style (PascalCase, SCREAMING_SNAKE_CASE, or camelCase), and select an output format. The generated code updates in real time. Toggle additional options like JSDoc comments, export keyword, type guards, or value arrays as needed. Copy the result to your clipboard with one click.
特征
- 多种输出格式 – String enum, numeric enum, const enum, as const object, and union type
- Key Naming Styles – PascalCase, SCREAMING_SNAKE_CASE, and camelCase with smart identifier generation
- JSDoc Comments – Optionally add documentation comments above each enum member
- Type Guard Generation – Create a type guard function to validate values at runtime
- Values Array – Generate an array of all enum values for iteration
- Reverse Mapping – Create a reverse lookup helper for numeric enums
- Smart Key Generation – Handles special characters, duplicates, values starting with numbers, and reserved words
- Real-Time Preview – Output updates instantly as you type or change options
常问问题
-
What is the difference between a string enum and a numeric enum in TypeScript?
String enums assign string values to each member, like Color.Red equals the string RED. Numeric enums assign incrementing numbers starting from 0 by default, so Color.Red equals 0 and Color.Blue equals 1. String enums are better for debugging because the values are readable in logs and network requests. Numeric enums are slightly more compact and support reverse mapping natively, meaning you can look up the name from the number. Most modern TypeScript projects prefer string enums for clarity.
-
When should I use const enum versus a regular enum?
Const enums are completely inlined at compile time. The TypeScript compiler replaces every reference to a const enum member with its literal value, so no enum object exists at runtime. This produces smaller bundle sizes and faster execution. However, const enums cannot be iterated over (no Object.values), do not support reverse mapping, and can cause issues with declaration files in library code. Use const enums for internal application constants where you want zero runtime overhead. Use regular enums when you need to iterate over values or when publishing a library.
-
What is an as const object and how does it compare to an enum?
An as const object uses a plain JavaScript object with a const assertion to achieve enum-like behavior. For example, const Color = { Red: 'RED', Blue: 'BLUE' } as const creates an immutable object where TypeScript infers the literal types. This approach works in both TypeScript and JavaScript, supports tree-shaking better than enums, and avoids some edge cases with enum transpilation. The trade-off is slightly more verbose type extraction: you need type Color = typeof Color[keyof typeof Color] to get the union type. Many teams now prefer as const objects over enums for these practical reasons.
-
What is a type guard and why would I generate one?
A type guard is a function that narrows a value's type at runtime. For an enum, a type guard checks whether a string or number is actually a valid member of the enum and tells TypeScript's type system about the result. For example, function isColor(value: string): value is Color returns true only if the value matches an enum member. This is useful when validating API responses, form inputs, or any external data where you need to confirm a value belongs to your enum before using it in type-safe code.
