Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. Download free →
CLSkills
Code Generationbeginner

Enum Generator

Share

Generate enums from constant values

Works with OpenClaude

You 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

  1. Collect all constant values into a structured format (array of objects with name and value keys)
  2. Validate that enum names are valid identifiers (alphanumeric, no spaces, match language conventions)
  3. Check for duplicate values—decide if you want aliased enums or error handling
  4. Determine the enum value type (string, number, or mixed) to set correct typing
  5. Generate the enum declaration with proper syntax for your target language
  6. Add helper functions for lookup operations if bidirectional access is needed
  7. Export the enum and any utility functions for use in other modules
  8. Generate TypeScript as const assertions 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

Quick Info

Difficultybeginner
Version1.0.0
AuthorClaude Skills Hub
codegenenumsconstants

Install command:

curl -o ~/.claude/skills/enum-generator.md https://claude-skills-hub.vercel.app/skills/code-generation/enum-generator.md

Related Code Generation Skills

Other Claude Code skills in the same category — free to download.

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.