Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
Back to tool/Back to issues
#64494·TypeScript

Decorated class gets `name === "_a"` when a `#private` field initializer references the class (regression in 7.0)

Author: bagbagCreated Sep 28, 2026Updated Sep 28, 2026

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

typescript
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:

javascript
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:

diff
-    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 #private field initializer that references the class.

    A public field with the same self-reference keeps the name, and so does a #private field without it. target: esnext is unaffected, because decorators are not downleveled there.

  • Impact: code that uses Class.name or context.name sees _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

View original on GitHubView discussion on GitHub