microsoft / microsoft/TypeScript
keyof on generics arbitrarily returns `string` rather than the inferred values depending on the generic
オープン
まだ誰も着手していません。
Bug
Domain: Index Types
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 132
説明
Bug Report
🔎 Search Terms
keyof generic string
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about it
⏯ Playground Link
Playground link with relevant code
💻 Code
export type Item = {
args?: Record<string, string>;
};
export type BaseSpec = Record<string, {
items: Item[];
}>;
export function genericFunction<Spec extends BaseSpec>(spec: Spec): keyof Spec {
return {} as any;
}
const AAAAA: `someKey` = genericFunction({
someKey: {
items: [{
args: {},
}, {
args: {
name: `foo`,
},
}],
},
});
🙁 Actual behavior
Type 'string' is not assignable to type '"someKey"'.
🙂 Expected behavior
No errors
💻 Additional Information
Weirdly, it seems to work if all args objects that are specified have exactly the same content. Removing an arg object seems to work, and it works as well if we add name: "foo" into the first arg object (or remove it from the second one).
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
リンク先の TypeScript Playground を開き、genericFunction、BaseSpec、および異なる args オブジェクトを使ってエラーを再現してください。このジェネリック推論のケースで keyof Spec が string に拡大される理由を調査し、その後、例が型チェックを通過し、推論された結果が "someKey" になることを確認してください。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 45/100