Microsoft/TypeScript

Distributed keyof over union leads to erroneous indexed access

Open

#49,000 建立於 2022年5月6日

在 GitHub 查看
 (11 留言) (1 反應) (0 負責人)TypeScript (6,726 fork)batch import
BugDomain: Indexed Access TypesHelp Wanted

倉庫指標

Star
 (48,455 star)
PR 合併指標
 (平均合併 6天 17小時) (30 天內合併 9 個 PR)

描述

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.

貢獻者指南