microsoft / microsoft/TypeScript
Inconsistent `typeof this` when the `this` parameter declared in the signature of a function parameter
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 132
説明
### 🔎 Search Terms
"this parameter", "typeof this"
### 🕗 Version & Regression Information
- This changed between versions 5.9.3 and 6.0.x
### ⏯ Playground Link
https://www.typescriptlang.org/play/?ts=6.0.3#code/JYOwLgpgTgZghgYwgAgILIN7LgLmQZzClAHNkBfAKEo0uWQHoHlRJZEUAhTZEgCjAALYPjyoAlHgBuAe2AATCnUbMAesgDKqALIBRZAAUASgHlOAGV3blraPCTJuWEngHDRaccgC8APmSyCkrKMACuIAhgwDIgyDB8APp4nJIBcooYVCF8tPT0-F65efRgAJ4ADigAGj7IZZUyMHXuysVMeercfPiCMqEANooARigSrUr05OLU9PFF+XyF4yUtxSUV1bX1EI3NIssqHWjjVJPT2fO8i5gH28g13nUbu0L7a4f06qi3q3mnFNMskUwhEojE4olXK8PBIfP5AvJUgjMFlZnwQZForECjc2swACobZDQKAyKCuABMADYABwAZi8AHJoYyWABbcr9YAIYBgfqlZCCOD4J6VZCMuAgUqskYIOChfAoXnIeQyCAikAyMCCuBSFBwUX6kCasBwMEgAB043aAAEwPgALQQAAelUiTqgpKg4zuD0NL1+UxmEIx5quS2K0J9RL92wDbzy7U+xzOwfioax4dxeV9W2eTSjeKO30jgcBlCAA
### 💻 Code
```ts
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`):
```jsonc
{
// 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,
}
}
```
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
リンクされた TypeScript Playground の再現コードから始め、5.9.3 と 6.0.3 の動作を比較し、コールバックの `this` パラメーターの文脈依存の型付けに注目してください。this 型の推論に関するコンパイラーテストを特定し、3 つのオブジェクトメソッドの例と単独のコールバックをカバーする回帰ケースを追加してください。`typeof this` が、実行時の `this` の使用を必要とせずに一貫して A に解決されれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 静か
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 48/100