microsoft / microsoft/TypeScript
Inconsistent `typeof this` when the `this` parameter declared in the signature of a function parameter
Chưa có ai nhận issue này.
- 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
"this parameter", "typeof this"
🕗 Version & Regression Information
- This changed between versions 5.9.3 and 6.0.x
⏯ Playground Link
💻 Code
interface A { a: string }
{
// interface B { g(this: A): void }
// ^ SAME PROBLEM
interface B { g: (this: A) => void }
function f(_: B): void {}
f({
g() {
type X = typeof this
// ^ B (should be A)
}
})
f({
g() {
this
type X = typeof this
// ^ A
}
})
f({
g() {
type X = typeof this
// ^ A
this
}
})
}
{
function f(_: (this: A) => void): void {}
f(function g() {
// Type error: (2683) 'this' implicitly has type 'any' because it does not have a type annotation.
// @ts-expect-error
type X = typeof this
})
f(function g() {
this
type X = typeof this
// ^ A
})
f(function g() {
type X = typeof this
// ^ A
this
})
}
🙁 Actual behavior
When declaring the this parameter in the signature of a function parameter (e.g., a callback function like function f(callback: (this: A) => void): void {}), the inferred typeof this in the function body can be inconsistent. If this is used in the function body, typeof this behaves as expected. If this is not used, typeof this is inferred as if the this parameter was never declared in the signature.
🙂 Expected behavior
The inferred typeof this should be consistent regardless of the usage of this in the function body.
Additional information about the issue
I can reproduce this problem also in @typescript/native-preview: 7.0.0-dev.20260705.1.
Locally I used the "default" tsconfig.json (tsc --init/tsgo --init):
{
// Visit https://aka.ms/tsconfig to read more about this file
"compilerOptions": {
// Environment Settings
// See also https://aka.ms/tsconfig/module
"module": "nodenext",
"target": "esnext",
"types": [],
// Other Outputs
"sourceMap": true,
"declaration": true,
"declarationMap": true,
// Stricter Typechecking Options
"noUncheckedIndexedAccess": true,
"exactOptionalPropertyTypes": true,
// Recommended Options
"strict": true,
"jsx": "react-jsx",
"verbatimModuleSyntax": true,
"isolatedModules": true,
"noUncheckedSideEffectImports": true,
"moduleDetection": "force",
"skipLibCheck": true,
}
}
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu với bản tái hiện được liên kết trong TypeScript Playground và so sánh hành vi giữa 5.9.3 và 6.0.3, tập trung vào việc định kiểu theo ngữ cảnh cho các tham số this của callback. Tìm các bài kiểm thử của trình biên dịch cho việc suy luận kiểu this và thêm một trường hợp hồi quy bao quát ba ví dụ về phương thức đối tượng và callback độc lập; được coi là hoàn tất khi typeof this luôn được phân giải thành A mà không yêu cầu sử dụng this trong thời gian chạy.
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
- Ít trao đổi
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 48/100