Skip to content

Stage 3 Class Method Decorators break references to classes declared on the module scopeΒ #53944

Closed
@nigelwtf

Description

@nigelwtf

Bug Report

πŸ”Ž Search Terms

decorator, decorators

πŸ•— Version & Regression Information

  • This is using version 5.0.4

⏯ Playground Link

Workbench Repro

πŸ’» Code

function myDecorator(target: any) {
    return function(this: any, ...args: any[]) {
        return target.apply(this, args);
    }
}

export class MyClass {
    @myDecorator
    public foo() {
        return 'bar';
    }
}

export const myClassInstance = new MyClass();

πŸ™ Actual behavior

Interestingly the Workbench doesn't reproduce the issue - but when running tsc locally, the outputted JS code looks like this:

exports.MyClass = (() => {
    var _a;
    let _instanceExtraInitializers = [];
    let _foo_decorators;
    return _a = class MyClass {
      ...
    },
    (() => {
        _foo_decorators = [someDecorator];
        __esDecorate(_a, null, _foo_decorators, { ... });
    })(),
    _a;
});

exports.myClass = new MyClass();

Because MyClass is now defined under the exports object, the last line fails :'(

This only happens when a member decorator is present in the class. When the decorator is omitted, MyClass is declared under the module scope, and then assigned to the exports object later.

πŸ™‚ Expected behavior

I should be able to instantiate an exported class with a stage 3 decorator from within the same module.

Metadata

Metadata

Assignees

Labels

Needs InvestigationThis issue needs a team member to investigate its status.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions