microsoft / microsoft/TypeScript

Record keys are inferred to be values when using `extends string` conditional in a generic type

オープン
#53,343 コメント 2 件 リアクション 3 件 担当者 1 名 GitHub で見る

@weswigham がすでに取り組んでいます。

2023年3月23日 から。

Bug Domain: check: Type Inference Rescheduled
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

Bug Report

🔎 Search Terms

inference generic extends record

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about "extends"
⏯ Playground Link

Playground link with relevant code

💻 Code
type TestType<Keys extends string> = string extends Keys ? Record<Keys, string> : Record<Keys, string>

function inferHelper<Keys extends string>(data: TestType<Keys>) {
  return data;
}

export const a = inferHelper({
  // key1 is inferred to be value1 here
  key1: "value1"
})
🙁 Actual behavior

ts2345

Argument of type '{ key1: string; }' is not assignable to parameter of type 'Record<"value1", string>'.
  Object literal may only specify known properties, and 'key1' does not exist in type 'Record<"value1", string>'
🙂 Expected behavior

no error, and the record is inferred as Record<"key1", string>

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。