Generate enums from constant values
✓Works with OpenClaudeYou are a code generation specialist. The user wants to generate enums from constant values (strings, numbers, or mixed types) and output them in their target language with proper typing and validation.
What to check first
- Identify the source of constants: hardcoded array, JSON file, API response, or database query
- Determine target language: TypeScript, Python, Java, Go, Rust, or C#
- Verify if you need bidirectional lookup (value → name and name → value)
Steps
- Collect all constant values into a structured format (array of objects with
nameandvaluekeys) - Validate that enum names are valid identifiers (alphanumeric, no spaces, match language conventions)
- Check for duplicate values—decide if you want aliased enums or error handling
- Determine the enum value type (string, number, or mixed) to set correct typing
- Generate the enum declaration with proper syntax for your target language
- Add helper functions for lookup operations if bidirectional access is needed
- Export the enum and any utility functions for use in other modules
- Generate TypeScript
as constassertions or equivalent immutability markers
Code
interface ConstantDef {
name: string;
value: string | number;
}
function generateEnum(
constants: ConstantDef[],
enumName: string,
language: "typescript" | "python" | "java" = "typescript"
): string {
// Validate enum names
const invalid = constants.filter(c => !/^[A-Z_][A-Z0-9_]*$/.test(c.name));
if (invalid.length > 0) {
throw new Error(`Invalid enum names: ${invalid.map(c => c.name).join(", ")}`);
}
if (language === "typescript") {
const hasStringValues = constants.some(c => typeof c.value === "string");
const hasNumberValues = constants.some(c => typeof c.value === "number");
const enumLines = constants.map(c => {
const valueStr = typeof c.value === "string" ? `"${c.value}"` : c.value;
return ` ${c.name} = ${valueStr},`;
});
return `export enum ${enumName} {
${enumLines.join("\n")}
}
export function get${enumName}Name(value: ${hasStringValues && hasNumberValues ? "string | number" : hasStringValues ? "string" : "number"}): string | null {
return Object.entries(${enumName}).find(([, v]) => v === value)?.[0] ?? null;
}`;
}
if (language === "python") {
const enumLines = constants.map(c => {
const valueStr = typeof c.value === "string" ? `"${c.value}"` : c.value;
return ` ${c.
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Code Generation Skills
Other Claude Code skills in the same category — free to download.
Type Generator
Generate TypeScript types from JSON/API responses
Interface from JSON
Generate interfaces from JSON samples
Boilerplate Reducer
Generate boilerplate code patterns
Regex Builder
Build and test regular expressions
SQL Generator
Generate SQL queries from natural language
Mock Data Generator
Generate realistic mock data (Faker.js)
Type Guard Generator
Generate TypeScript type guards
Regex Lookahead
Write regex with lookaheads, lookbehinds, and named groups
Want a Code Generation skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.