Skip to content

Incorrect reference this in static context with experimentalDecorators enabledΒ #63189

@yavanosta

Description

@yavanosta

πŸ”Ž Search Terms

"static this", "experimental decorators"

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about static

⏯ Playground Link

https://www.typescriptlang.org/play/?experimentalDecorators=true&target=7&ts=5.9.3&emitDecoratorMetadata=true#code/GYVwdgxgLglg9mABAEQKYTgJwIZS4gCim0wHNUoAuRAMXGnjABpEBrVAT2oGcpMYwpRAB9E4ACapgA1OJYDJAD2pgQAWwBGqTAEpEAbwC+AKGMQANtm7dEAWQ4BhADJWb+44kS9cMCIkyo2OII5hxexLAQACoAFjDcAEpSiAC8iFBx3ADcHogYYLyYINBYBAACaBg4eJiIwHBwKupaugYmJkA

πŸ’» Code

function Decorator (target: Function, key: string | undefined, index: number) {}

class MyClass {
  static readonly staticThisRef = this;
  constructor(@Decorator foo: number) {}
}

πŸ™ Actual behavior

MyClass.staticThisRef = (void 0);

πŸ™‚ Expected behavior

staticThisRef points to the class.

Additional information about the issue

Enabling or disabling experimentalDecorators fixes the issue.

With experimentalDecorators:

let MyClass = class MyClass {
    constructor(foo) { }
};
MyClass.staticThisRef = (void 0);
MyClass = __decorate([
    __param(0, Decorator)
], MyClass);

Without experimentalDecorators:

class MyClass {
    constructor(foo) { }
}
_a = MyClass;
MyCLass.staticThisRef = _a;

However only experimental decorators support decorating parameters.

Also TypeScript compiler behavior here is different from esbuild with experimentalDecorators feature enabled:

https://esbuild.github.io/try/#dAAwLjI3LjMALS1sb2FkZXI9dHMgIi0tdHNjb25maWctcmF3PXtcImNvbXBpbGVyT3B0aW9uc1wiOntcImV4cGVyaW1lbnRhbERlY29yYXRvcnNcIjp0cnVlfX0iAGZ1bmN0aW9uIERlY29yYXRvciAodGFyZ2V0OiBGdW5jdGlvbiwga2V5OiBzdHJpbmcgfCB1bmRlZmluZWQsIGluZGV4OiBudW1iZXIpIHt9CgpjbGFzcyBNeUNMYXNzIHsKICBzdGF0aWMgcmVhZG9ubHkgc3RhdGljVGhpc1JlZiA9IHRoaXM7CiAgY29uc3RydWN0b3IoQERlY29yYXRvciBmb286IG51bWJlcikge30KfQ

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: DecoratorsThe issue relates to the decorator syntax

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions