Microsoft/TypeScript
GitHub ã§èŠãDistributed keyof over union leads to erroneous indexed access
Open
#49,000 opened on 2022幎5æ6æ¥
BugDomain: Indexed Access TypesHelp Wanted
Repository metrics
- Stars
- Â (48,455 stars)
- PR merge metrics
-  (å¹³åããŒãž 6d 17h) (30d ã§ 9 merged PRs)
説æ
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.