{"record":{"id":"ae45fa848714b20d","repo":"microsoft/TypeScript","slug":"function-expected","errorCode":null,"errorMessage":"Function expected","messagePattern":"Function expected","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/compiler/factory/emitHelpers.ts","lineNumber":774,"sourceCode":"    name: \"typescript:param\",\r\n    importName: \"__param\",\r\n    scoped: false,\r\n    priority: 4,\r\n    text: `\r\n            var __param = (this && this.__param) || function (paramIndex, decorator) {\r\n                return function (target, key) { decorator(target, key, paramIndex); }\r\n            };`,\r\n};\r\n\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","sourceCodeStart":756,"sourceCodeEnd":792,"githubUrl":"https://github.com/microsoft/TypeScript/blob/b465fdbfe175304d9b977da137b2c178ae1091d3/src/compiler/factory/emitHelpers.ts#L756-L792","documentation":"TypeScript emits `__esDecorate` into downleveled output to support TC39 decorators on older runtimes. Its internal `accept(f)` rejects any value that is neither `undefined` nor a function — it validates decorator return values (get/set/init/value) and addInitializer callbacks. A decorator that returns a defined non-function triggers this TypeError at runtime.","triggerScenarios":"A class-element decorator returns a non-function value (e.g. a number, string, or object) for a method/field/value; or `context.addInitializer(<non-function>)` is called.","commonSituations":"Porting a decorator that returns the wrong shape; a buggy third-party decorator library; calling addInitializer with the result of an invocation instead of a function reference.","solutions":["Make the decorator return either `undefined` or a function for the decorated kind.","Pass a function reference to addInitializer (not the result of calling one).","Raise `target` to a runtime with native decorator support to drop the helper.","Unit-test decorators against the emitted helper to catch shape violations early."],"exampleFix":"// before\nfunction deco(value, ctx) { return 42; }   // non-function -> \"Function expected\"\nclass A { @deco m() {} }\n\n// after\nfunction deco(value, ctx) { return () => {}; }  // function (or undefined)\nclass A { @deco m() {} }","handlingStrategy":"type-guard","validationCode":"function validateDecoratorResult(v: unknown): void {\n  if (v !== undefined && typeof v !== \"function\") {\n    throw new TypeError(\"Decorator must return undefined or a function for this kind.\");\n  }\n}","typeGuard":"function isAcceptedDecoratorValue(v: unknown): v is undefined | Function {\n  return v === undefined || typeof v === \"function\";\n}","tryCatchPattern":null,"preventionTips":["Return either undefined or a function from class-element decorators.","Pass function references (not call results) to context.addInitializer.","Add unit tests that exercise decorators against the emitted __esDecorate helper."],"tags":["decorators","runtime","emit-helpers","es-decorators"],"backgroundTag":null,"analyzedSha":"b465fdbfe175304d9b977da137b2c178ae1091d3","analyzedAt":"2026-08-12T05:38:42.698Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}