Microsoft/TypeScript

Distributed keyof over union leads to erroneous indexed access

Open

#49.000 geöffnet am 6. Mai 2022

Auf GitHub ansehen
 (11 Kommentare) (1 Reaktion) (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

Bug Report

🕗 Version & Regression Information

This is the behavior in every version I tried

⏯ Playground Link

Playground link with relevant code

💻 Code

type KeyOf<T> = T extends unknown ? keyof T : never

type Wrong<T> = { [K in KeyOf<T>]: T[K] }

🙁 Actual behavior

It is accepted by the type checker, but it shouldn't be allowed because T could be a union of objects with unrelated properties. The computed type doesn't even have any sense:

type test= Wrong<{ prop0: string } | { prop1: number; prop2: boolean } | { prop3: string[] }>
//   { prop0: unknown; prop1: unknown; prop2: unknown; prop3: unknown; }

🙂 Expected behavior

K shouldn't be allowed to index type T.

Contributor Guide