microsoft/TypeScript · error · TypeError
Object expected
Error message
Object expected
What it means
For `accessor`-kind decorated elements, `__esDecorate` requires a non-undefined decorator return value to be an object (with optional get/set/init). Returning a primitive throws "Object expected".
Source
Thrown at src/compiler/factory/emitHelpers.ts:787
importName: "__esDecorate",
scoped: false,
priority: 2,
text: `
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
var _, done = false;
for (var i = decorators.length - 1; i >= 0; i--) {
var context = {};
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
if (kind === "accessor") {
if (result === void 0) continue;
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
if (_ = accept(result.get)) descriptor.get = _;
if (_ = accept(result.set)) descriptor.set = _;
if (_ = accept(result.init)) initializers.unshift(_);
}
else if (_ = accept(result)) {
if (kind === "field") initializers.unshift(_);
else descriptor[key] = _;
}
}
if (target) Object.defineProperty(target, contextIn.name, descriptor);
done = true;
};`,
};
const runInitializersHelper: UnscopedEmitHelper = {
name: "typescript:runInitializers",
importName: "__runInitializers",
scoped: false,
View on GitHub (pinned to b465fdbfe1)
Solutions
- Return `undefined` or an object `{ get?, set?, init? }` from accessor decorators.
- Use a decorator specifically written for the accessor kind (check ctx.kind === "accessor").
- Raise target to a runtime with native decorators to avoid the helper.
Example fix
// before
function acc(v, ctx) { return 42; } // non-object -> "Object expected"
class A { @acc accessor x = 1; }
// after
function acc(v, ctx) { return { init: () => 2 }; }
class A { @acc accessor x = 1; } Defensive patterns
Strategy: type-guard
Validate before calling
function validateAccessorResult(v: unknown): void {
if (v !== undefined && (v === null || typeof v !== "object")) {
throw new TypeError("Accessor decorator must return undefined or an object.");
}
} Type guard
function isAccessorDescriptor(v: unknown): v is { get?: Function; set?: Function; init?: Function } {
return v === undefined || (typeof v === "object" && v !== null);
} Prevention
- Return undefined or { get?, set?, init? } from accessor decorators.
- Branch on ctx.kind so the right shape is returned per kind.
When it happens
Trigger: An accessor decorator returns a number, string, boolean, or any non-object value (other than undefined).
Common situations: Misunderstanding the accessor-decorator contract; reusing a method/field decorator on an accessor.
Related errors
- Function expected
- Cannot add initializers after decoration has completed
- Private accessor was defined without a getter
- Private accessor was defined without a setter
- Symbol.asyncIterator is not defined.
AI-assisted analysis of microsoft/TypeScript@b465fdbfe1 (2026-08-12).
Data as JSON: /api/errors/6484ec62d363c772.
Report an issue: GitHub.