Skip to content

Using static members in class decoratorΒ #57366

@Sayan751

Description

@Sayan751

πŸ”Ž Search Terms

TC39 decorators, class decorator, static members

πŸ•— Version & Regression Information

This changed when started using the new TC39 decorators (non-experimental/legacy).

⏯ Playground Link

https://www.typescriptlang.org/play?emitDecoratorMetadata=true&target=99&lib=lib.esnext.d.ts%2Clib.esnext.decorators.d.ts%2Clib.dom.d.ts&ts=&ssl=9&ssc=1&pln=2&pc=1#code/PTAEAEBsEsCMC5QFMDOA7JAPALgGlRjgHQAmSAxgPYBOAhtjSriZQLYCwAUFwGYCuactmiU0oMlQAUAC0qUA1gH5EtNAE8AlCvWgA3qAC+XLuAmVJAMTlFYtahq7lItFClBXKerqFAAHPrAw5KAo2PTQwbbUkhp6hlxGnEA

πŸ’» Code

function deco(hook?: any): any { }

@deco(Foo.bar)
class Foo {
  public static bar() { }
}

πŸ™ Actual behavior

Getting the following error when the generated JS is run.

Cannot read properties of undefined (reading 'bar')

πŸ™‚ Expected behavior

The generated code should work. Otherwise, the tsc should at least throw compilation error.

Additional information about the issue

This happens because the class decorators are listed, before the class is defined.

let Foo = (() => {
    let _classDecorators = [deco(Foo.bar)]; // this seems to be causing the issue.
    let _classDescriptor;
    let _classExtraInitializers = [];
    let _classThis;
    var Foo = 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);
        }
        static bar() { }
    };
    return Foo = _classThis;
})();

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions