Decorated class gets `name === "_a"` when a `#private` field initializer references the class (regression in 7.0)
Search Terms
decorator class name _a, ES decorators private field class name, var Foo = _a = class, NamedEvaluation class name decorator, ClassDecoratorContext.name wrong, class .name regression 7.0, __esDecorate _classThis.name
Version & Regression Information
- This changed between versions 6.0.3 and 7.0.1-rc. Also reproduces on 7.0.2 and 7.1.0-dev.20260926.1.
⏯ Playground Link
TS 7 doesn't work on Playground
Code
function dec(_target: unknown, _context: ClassDecoratorContext): void {}
@dec
class Foo {
#self = Foo;
}
console.log(Foo.name);No compiler options are needed: tsc with { "files": ["a.ts"] } (default target), then node a.js.
Actual behavior
Prints _a. 7.0 emits the class expression through a chained assignment to the temporary _a:
let Foo = (() => {
var _a;
let _classDecorators = [dec];
let _classDescriptor;
let _classExtraInitializers = [];
let _classThis;
var Foo = _a = class {
static { _classThis = this; }
static {
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
Foo = _classThis = _classDescriptor.value;
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
__runInitializers(_classThis, _classExtraInitializers);
}
#self = Foo;
};
return Foo = _classThis;
})();The anonymous class is the right-hand side of _a = …, so NamedEvaluation names it _a. Foo.name is "_a". So is ClassDecoratorContext.name, because the decorator context is built from _classThis.name. _a is never read afterwards.
Expected behavior
Prints Foo, as 6.0.3 does. 6.0.3's emit differs only in this line:
- var Foo = _a = class {
+ var Foo = class {The emitted class's name, and the decorator context's name, should be the declared class name.
Additional information about the issue
Reproducing needs both conditions below. With either one alone, 7.0 emits
var Foo = class {and the name is correct:- an ES (standard) class decorator;
- a
#privatefield initializer that references the class.
A public field with the same self-reference keeps the name, and so does a
#privatefield without it.target: esnextis unaffected, because decorators are not downleveled there.Impact: code that uses
Class.nameorcontext.namesees_a, for example logger categories, diagnostics and DI registries.Possible fixes: don't chain the unused temporary into the class expression, or emit
__setFunctionName(_classThis, "Foo"), as is already done elsewhere.Related, but not this bug: #54607, #44908, #48515.
Source: microsoft/TypeScript