{"record":{"id":"f35b1e86c524b83e","repo":"microsoft/TypeScript","slug":"cannot-add-initializers-after-decoration-has-compl","errorCode":null,"errorMessage":"Cannot add initializers after decoration has completed","messagePattern":"Cannot add initializers after decoration has completed","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/compiler/factory/emitHelpers.ts","lineNumber":783,"sourceCode":"\r\n// ES Decorators Helpers\r\nconst esDecorateHelper: UnscopedEmitHelper = {\r\n    name: \"typescript:esDecorate\",\r\n    importName: \"__esDecorate\",\r\n    scoped: false,\r\n    priority: 2,\r\n    text: `\r\n        var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {\r\n            function accept(f) { if (f !== void 0 && typeof f !== \"function\") throw new TypeError(\"Function expected\"); return f; }\r\n            var kind = contextIn.kind, key = kind === \"getter\" ? \"get\" : kind === \"setter\" ? \"set\" : \"value\";\r\n            var target = !descriptorIn && ctor ? contextIn[\"static\"] ? ctor : ctor.prototype : null;\r\n            var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});\r\n            var _, done = false;\r\n            for (var i = decorators.length - 1; i >= 0; i--) {\r\n                var context = {};\r\n                for (var p in contextIn) context[p] = p === \"access\" ? {} : contextIn[p];\r\n                for (var p in contextIn.access) context.access[p] = contextIn.access[p];\r\n                context.addInitializer = function (f) { if (done) throw new TypeError(\"Cannot add initializers after decoration has completed\"); extraInitializers.push(accept(f || null)); };\r\n                var result = (0, decorators[i])(kind === \"accessor\" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);\r\n                if (kind === \"accessor\") {\r\n                    if (result === void 0) continue;\r\n                    if (result === null || typeof result !== \"object\") throw new TypeError(\"Object expected\");\r\n                    if (_ = accept(result.get)) descriptor.get = _;\r\n                    if (_ = accept(result.set)) descriptor.set = _;\r\n                    if (_ = accept(result.init)) initializers.unshift(_);\r\n                }\r\n                else if (_ = accept(result)) {\r\n                    if (kind === \"field\") initializers.unshift(_);\r\n                    else descriptor[key] = _;\r\n                }\r\n            }\r\n            if (target) Object.defineProperty(target, contextIn.name, descriptor);\r\n            done = true;\r\n        };`,\r\n};\r\n\r","sourceCodeStart":765,"sourceCodeEnd":801,"githubUrl":"https://github.com/microsoft/TypeScript/blob/b465fdbfe175304d9b977da137b2c178ae1091d3/src/compiler/factory/emitHelpers.ts#L765-L801","documentation":"In `__esDecorate`, each decorator's context.addInitializer pushes into an extraInitializers array; once the decoration loop completes the helper sets `done = true`. Calling addInitializer afterwards throws, matching the spec rule that initializers must be registered synchronously during decoration.","triggerScenarios":"A decorator captures context.addInitializer and invokes it after the decoration loop has finished — e.g. from a later microtask, an async callback, or from within a deferred initializer.","commonSituations":"Decorator that defers registration to setTimeout/Promise resolution; storing addInitializer for reuse; re-entering it from another decorator's later phase.","solutions":["Call context.addInitializer synchronously inside the decorator body.","Do not store or capture the addInitializer function for deferred use.","If you need lazy behavior, register a single function that does the deferred work when invoked."],"exampleFix":"// before\nfunction deco(v, ctx) {\n  setTimeout(() => ctx.addInitializer(() => console.log(\"hi\")), 0); // throws after done\n}\n\n// after\nfunction deco(v, ctx) {\n  ctx.addInitializer(() => console.log(\"hi\"));   // register synchronously\n}","handlingStrategy":"validation","validationCode":"// Register initializers synchronously during decoration only.\nfunction deco(value: unknown, ctx: ClassMemberDecoratorContext) {\n  ctx.addInitializer(() => { /* ... */ });  // OK: synchronous, inside decorator\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never capture or defer context.addInitializer; call it inside the decorator body.","If deferred work is needed, register a single function that performs it when invoked."],"tags":["decorators","runtime","emit-helpers","initializers"],"backgroundTag":null,"analyzedSha":"b465fdbfe175304d9b977da137b2c178ae1091d3","analyzedAt":"2026-08-12T05:38:42.698Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}