{"record":{"id":"cdf656cac596a3c0","repo":"BoundaryML/baml","slug":"enum-name-already-exists","errorCode":null,"errorMessage":"Enum ${name} already exists","messagePattern":"Enum (.+?) already exists","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"engine/language_client_typescript/type_builder.js","lineNumber":72,"sourceCode":"    }\n    classViewer(name, properties) {\n        return new ClassViewer(this.tb, name, new Set(properties));\n    }\n    classBuilder(name, properties) {\n        return new ClassBuilder(this.tb, name, new Set(properties));\n    }\n    enumViewer(name, values) {\n        return new EnumViewer(this.tb, name, new Set(values));\n    }\n    enumBuilder(name, values) {\n        return new EnumBuilder(this.tb, name, new Set(values));\n    }\n    addClass(name) {\n        if (this.classes.has(name)) {\n            throw new Error(`Class ${name} already exists`);\n        }\n        if (this.enums.has(name)) {\n            throw new Error(`Enum ${name} already exists`);\n        }\n        this.classes.add(name);\n        return new ClassBuilder(this.tb, name);\n    }\n    addEnum(name) {\n        if (this.classes.has(name)) {\n            throw new Error(`Class ${name} already exists`);\n        }\n        if (this.enums.has(name)) {\n            throw new Error(`Enum ${name} already exists`);\n        }\n        this.enums.add(name);\n        return new EnumBuilder(this.tb, name);\n    }\n    addBaml(baml) {\n        this.tb.addBaml(baml, this.runtime);\n    }\n}","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/language_client_typescript/type_builder.js#L54-L90","documentation":"TypeBuilder.addClass(name) throws when the name is already taken by an enum registered on the same TypeBuilder, keeping class and enum namespaces disjoint. Duplicate or shadowing names would make generated BAML ambiguous.","triggerScenarios":"Calling addClass with a name previously passed to addEnum on the same builder; mixing dynamic class creation with a fixed enum list that shares names.","commonSituations":"Schema-generation code where enums and classes derive from the same source list of names; migrating code from string-typed enums to classes (or vice versa) without renaming.","solutions":["Rename the class or the enum so names are unique within the TypeBuilder","Track registered names in one Set covering both classes and enums before registering","Check with your own registry before calling addClass if the name may collide with enums","Wrap in try/catch and choose a suffixed name on collision"],"exampleFix":"// before\ntb.addEnum(\"Status\", [\"A\", \"B\"]);\ntb.addClass(\"Status\"); // throws\n// after\ntb.addEnum(\"Status\", [\"A\", \"B\"]);\ntb.addClass(\"StatusClass\");","handlingStrategy":"validation","validationCode":"if (enumNames.has(name)) throw new Error(`'${name}' is already an enum`);","typeGuard":"null","tryCatchPattern":"try { tb.addClass(name); } catch (e) {\n  if (String(e.message).includes('already exists')) name = name + 'Class';\n  else throw e;\n}","preventionTips":["Keep class and enum names in disjoint namespaces by convention (e.g. enums suffixed 'Kind')","Check combined name registry before any add* call","Derive class/enum names with distinct prefixes in generators"],"tags":["javascript","type-builder","duplicate","namespace-collision","baml"],"backgroundTag":"file-already-exists","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}