microsoft / microsoft/TypeScript
Incorrect reference `this` in static context with `experimentalDecorators` enabled
- Ngôn ngữ chính
- Go
- Star
- 111k
- Fork
- 14.3k
- Merge trung bình
- 2 ngày 4 giờ
- Pull request đã merge (30 ngày)
- 132
Mô tả
### 🔎 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
Hướng dẫn đóng góp
Hướng nghiên cứu
Bắt đầu với ví dụ TypeScript Playground được cung cấp và so sánh đầu ra được phát ra khi có và không có experimentalDecorators. Điều tra đường dẫn của trình biên dịch phát ra tham chiếu this tĩnh cho một lớp có tham số hàm khởi tạo được gắn decorator. Hoàn thành khi đầu ra experimental-decorator khiến staticThisRef trỏ đến MyClass, đồng thời vẫn giữ hỗ trợ gắn decorator cho tham số.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- typescript
- Lĩnh vực
- compilers
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 38/100