microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.7k stars 12.45k forks source link

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

Open dk1a opened 1 year ago

dk1a commented 1 year ago

Bug Report

🔎 Search Terms

inference generic extends record

🕗 Version & Regression Information

⏯ 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>

RyanCavanaugh commented 1 year ago

3.8 -> 3.9 regression, actually

typescript-bot commented 1 year ago

The change between origin/release-3.6 and origin/release-4.0 occurred at 47b60ece0baddaa7e354881cd537a61335b200d6.