microsoft / microsoft/TypeScript
Incorrect inference/autocompletion on generic arrays, when values can be inferred from a defined object.
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
TypeScript Version: 4.1.2
Search Terms:
Autocompletion, incorrect, values, inference, generic, array, object, keys
Summary:
When an interface/a type has an object with generic keys, and an array of those keys, the array values cannot be infered from the object keys.
Code
interface Recipe<INGREDIENTS extends string> {
quantities: Record<INGREDIENTS, number>
allergens?: INGREDIENTS[]
}
function createRecipe<INGREDIENTS extends string>(recipe: Recipe<INGREDIENTS>) {}
createRecipe({
quantities: {
eggs: 1,
flour: 2,
},
allergens: ['']
})
Expected behavior:
Here, when trying to give a value to allergens, the autocompletion should show "eggs" | "flour".
Actual behavior:
The autocompletion doesn't find anything.

Notes:
The other way is working: you can fill the array first, then the object keys will autocomplete - but this rarely make sense to write things that way.
Failed workarounds:
This bug is still present, even when:
allergensis optionnal- We switch from an interface to a type
- We use
keyof this['quantities']instead ofINGREDIENTS[]
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、issue にある TypeScript 4.1.2 の例を再現し、quantities オブジェクトから allergens 配列への推論と、その自動補完結果に注目します。報告された動作を逆順の場合と比較し、例のジェネリック型を変更せずに allergens の補完候補として "eggs" と "flour" が提示されれば、issue は完了したものとします。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 静か
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 38/100