Microsoft/TypeScript

Literal tested as key of a certain type cannot be used as an index type

Open

#57.109 geöffnet am 20. Jan. 2024

Auf GitHub ansehen
 (1 Kommentar) (2 Reaktionen) (0 zugewiesene Personen)TypeScript (6.726 Forks)batch import
BugDomain: Indexed Access TypesHelp Wanted

Repository-Metriken

Stars
 (48.455 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 6T 17h) (9 gemergte PRs in 30 T)

Beschreibung

🔎 Search Terms

"literal", "extends", "keyof", "cannot be used to index type", "conditional"

🕗 Version & Regression Information

Versions: ^5.0 (regression)

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.3.3&ssl=7&ssc=54&pln=1&pc=1#code/C4TwDgpgBAKlC8UDeBYAUFKBrAXFAziALYBGA9gDYA06mZwAFhAE57mUQCGAdlVAPT8oPek2bYIIAhAhF8UZhACOAVwCWigCZQAZmXGNoJFQHMowMsLCROzdAF906QbHDQA5ACIsn91ADGPNz0UCTQKvgQ2pzyPFBq3JoQAB7mbgB0ABQATACsAMwAHACU6KCQUADCCFDuWH4pwBCJ8liSZDqwUAD8sADade4AulB43BAAbiwA3AJCPCBAA

💻 Code

type T = {
  k: symbol,
  other: boolean, // another key seems required for the bug to appear
}

// Type '"k"' cannot be used as an index type.(2538)
type C = 'k' extends keyof T ? T['k'] : never; // any

🙁 Actual behavior

An error is produced in the positive clause of the conditional type. The resulting type is incorrect.

🙂 Expected behavior

No error.

Additional information about the issue

No response

Contributor Guide