microsoft / microsoft/TypeScript

Computed type key inference inconsistent with identical explicitly declared type

オープン
#59,987 コメント 2 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Bug Domain: Mapped Types Help Wanted
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

🔎 Search Terms

keyof required keys key evaluation reduce getReducedType

🕗 Version & Regression Information
  • This is the behavior in every version I tried
⏯ Playground Link

https://tsplay.dev/WKQkzN

💻 Code
type distilled = distill<{
	//   ^?
	foo: "default"
	bar?: number
}>

// incorrectly inferred as unknown
type requiredDistilledKey = requiredKeyOf<distilled>
//   ^?

// explicit declaration identical to hover of distilled
type declared = {
	bar?: number
} & {
	foo?: "default"
}

// now correctly inferred as never
type requiredDeclaredKey = requiredKeyOf<declared>
//   ^?

type requiredKeyOf<o> = {
	[k in keyof o]-?: o extends { [_ in k]-?: o[k] } ? k : never
}[keyof o]

type distill<t> = t extends object ? distillMappable<t> : t

type distillMappable<o> = {
	[k in keyof o as k extends inferredDefaultKeyOf<o> ? never : k]: distill<o[k]>
} & {
	[k in inferredDefaultKeyOf<o>]?: distill<o[k]>
}

type inferredDefaultKeyOf<o> = {
	[k in keyof o]: o[k] extends "default" ? k : never
}[keyof o]


🙁 Actual behavior

requiredDistilledKey inferred as unknown

🙂 Expected behavior

requiredDistilledKey inferred as never, consistent with the equivalent declared version

Additional information about the issue

No response

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

リンク先の TypeScript Playground で、提供されている distill、requiredKeyOf、requiredDeclared の例を使って不一致を再現してください。計算された Mapped/Intersection 型がどのように縮約され、requiredKeyOf がそれをどのように評価するかを追跡してください。requiredDistilledKey が requiredDeclaredKey と一致する never として推論されれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。