microsoft / microsoft/TypeScript
this type in conditional type false clause is incorrectly typed
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 132
説明
TypeScript Version: 3.7.3
Search Terms:
generic type this widening incorrect
Code
type Option1 = { run(this: { b: 1 }): void }
function f1(options: Option1) { return options }
// `this` is correctly typed as `{ b: 1 }`
f1({ run() { this.b } })
type Option2<Config> = Config extends Record<string, any> ? {
config: Config,
run(this: Config): void
} : {
run(arg: { b: 1 }): void
}
function f2<Config>(options: Option2<Config>) { return options }
// `this` is correctly typed as `{ a: number }`
f2({ config: { a: 1 }, run() { this.a } })
// `arg` is correctly typed as `{ b: 1 }`
f2({ run(arg) { arg.b } })
type Option3<Config> = Config extends Record<string, any> ? {
config: Config,
run(this: Config): void
} : {
run(this: { b: 1 }): void
}
function f3<Config>(options: Option3<Config>) { return options }
// `this` is correctly typed as `{ a: number }`
f3({ config: { a: 1 }, run() { this.a } })
// `this` is widen to what `Config` extends to. In this case `Record<string, any>`
f3({ run() { this.b } })
Expected behavior:
this in f3({ run() { this.b } }) should be typed as in this in f1() and arg in f2()
Actual behavior:
this in f3({ run() { this.b } }) is typed as the base type of Config (Record<string,any> in the example above)
Playground Link:
Playground Link
Related Issues:
Some maybe related issues:
https://github.com/microsoft/TypeScript/issues/32990
https://github.com/microsoft/TypeScript/issues/30152
https://github.com/microsoft/TypeScript/issues/13995
I also recall there were some discussion about type and Record<>, but that is in the gitter channel and I can't find relevant issue or doc about it.
I think this is different than the related issues above because this does not involve union type.
This is about the false condition of the conditional type Config extends Record<string, any> ? ... : ... do not use the generic type Config and should not be affected by it.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
リンクされている TypeScript Playground の例を再現し、f1、f2、f3 における this のコンテキスト型を比較します。false 節に対する conditional type のコンテキスト型付けを調査します。f3 のコールバックの this が拡張されたベース型ではなく { b: 1 } として型付けされ、他の例にリグレッションが発生しないことが完了の条件です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100