{"id":"3fc3363514304ef6","repo":"babel/babel","slug":"cannot-initialize-the-same-private-elements-twice","errorCode":null,"errorMessage":"Cannot initialize the same private elements twice on an object","messagePattern":"Cannot initialize the same private elements twice on an object","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/babel-helpers/src/helpers/checkPrivateRedeclaration.ts","lineNumber":8,"sourceCode":"/* @minVersion 7.14.1 */\n\nexport default function _checkPrivateRedeclaration(\n  obj: object,\n  privateCollection: WeakMap<object, unknown> | WeakSet<object>,\n) {\n  if (privateCollection.has(obj)) {\n    throw new TypeError(\n      \"Cannot initialize the same private elements twice on an object\",\n    );\n  }\n}\n","sourceCodeStart":1,"sourceCodeEnd":13,"githubUrl":"https://github.com/babel/babel/blob/06b6eae39da4ce0689fad64e2c48a6375a464208/packages/babel-helpers/src/helpers/checkPrivateRedeclaration.ts#L1-L13","documentation":"Runtime helper _checkPrivateRedeclaration, emitted when transpiling private fields/methods. Before initializing the per-instance WeakMap/WeakSet for a class's private collection, it asserts that this object has not already been initialized; if the private collection already has the object, it throws a TypeError. This enforces the spec rule that the same private fields cannot be installed twice on one instance.","triggerScenarios":"At runtime, the private-field initialization code runs twice against the same instance. This usually happens when a constructor (or field setup) is invoked twice on one object — e.g. manually re-running class field initialization, double inheritance of the same private collection, or a transpilation bug producing duplicate init calls. The `privateCollection.has(obj)` check at checkPrivateRedeclaration.ts:7 fires.","commonSituations":"Calling a transpiled constructor's internal initializer twice (Reflect.construct misuse, manual constructor chaining); a mixin pattern that re-applies private fields; running two different transpiled versions of the same class against one object; corrupt hand-written ES5 shim of a class with private members.","solutions":["Do not manually re-invoke the constructor or field initializer on an existing instance — use `new` or Reflect.construct once.","Audit mixin/inheritance patterns that copy private-field setup; ensure each instance is initialized exactly once.","Make sure only one compiled copy of the class is loaded (dedupe the package) so two initializer closures do not run on the same object.","If the error appears after a Babel upgrade, check for duplicate transpilation (build pipeline transpiling twice)."],"exampleFix":"// before\ninitPrivateFields(obj); // first call\ninitPrivateFields(obj); // second call -> throws\n// after\ninitPrivateFields(obj); // call exactly once, ideally inside the constructor","handlingStrategy":"validation","validationCode":"// Use a WeakSet to ensure single initialization\nconst inited = new WeakSet();\nfunction initOnce(obj) {\n  if (inited.has(obj)) throw new TypeError('already initialized');\n  inited.add(obj); /* ... */\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Initialize each instance exactly once, inside the constructor.","Dedupe the compiled class package so two init closures do not run on one object.","Avoid mixins that re-apply private-field setup."],"tags":["runtime-helper","private-fields","initialization","spec-compliance"],"analyzedSha":"06b6eae39da4ce0689fad64e2c48a6375a464208","analyzedAt":"2026-08-03T20:13:43.465Z","schemaVersion":2}