microsoft / microsoft/TypeScript
Incorrect reference `this` in static context with `experimentalDecorators` enabled
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
### 🔎 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
```ts
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`:
```ts
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the provided TypeScript Playground example and compare the emitted output with and without experimentalDecorators. Investigate the compiler path that emits the static this reference for a class with a decorated constructor parameter. Done means the experimental-decorator output makes staticThisRef point to MyClass while preserving parameter decoration support.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100