microsoft / microsoft/TypeScript

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

Offen
#53,343 2 Kommentare 3 Reaktionen 1 zugewiesene Person Auf GitHub ansehen

@weswigham arbeitet bereits daran.

Seit 23.3.2023.

Bug Domain: check: Type Inference Rescheduled
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

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>

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.