microsoft / microsoft/TypeScript

Incorrect reference `this` in static context with `experimentalDecorators` enabled

未关闭
#63,189 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Bug Domain: Decorators
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

### 🔎 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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从提供的 TypeScript Playground 示例开始,比较启用和不启用 experimentalDecorators 时生成的输出。调查为具有已装饰构造函数参数的类生成静态 this 引用的编译器路径。完成的标准是:experimental-decorator 输出使 staticThisRef 指向 MyClass,同时保留参数装饰支持。

由索引模型根据 Issue 内容生成。

评估

技术栈
typescript
领域
compilers
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
38/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。