microsoft / microsoft/TypeScript
{} and { [K in never]: any } exhibit different simplification behavior
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
Bug Report
Under some circumstances, TypeScript treats { [K in never]: any } & { foo?: number } differently than {} & { foo?: number }. However, this does not occur with { [K in never]: any } & { foo: number }.
🔎 Search Terms
empty mapped object type, intersection with optional object, K in never
🕗 Version & Regression Information
Tested on 4.0.5, 4.1.5, 4.2.0-beta, and the current nightly
⏯ Playground Link
💻 Code
type AreAllStringsTs<T> = string extends T ? true : false;
type Test1 = AreAllStringsTs<{ foo?: number }>; // expected: false, actual: false
type Test2 = AreAllStringsTs<{ foo: number }>; // expected: false, actual: false
type Test3 = AreAllStringsTs<string | number>; // expected: true, actual: true
type Test4 = AreAllStringsTs<{ [K in never]: any } & { foo?: number }>; // expected: false, actual: true <-- FAIL
type Test5 = AreAllStringsTs<{ [K in never]: any } & { foo: number }>; // expected: false, actual: false
🙁 Actual behavior
Test4 is true.
🙂 Expected behavior
Test4 is false.
Potential Workaround
If you have a mapped object type in which the type expression for the keys might reduce to never, add a conditional check to see if the keys set is empty. For example, I encountered this when working with a type that would make all keys of an object that could take on the value undefined optional:
type Optionalize<T> =
{ [K in RequiredKeys<T>]: T[K] }
& { [K in NonRequiredKeys<T>]?: T[K] };
Adding a check for never made the behavior consistent with what I was expecting when every field of the input type accepted undefined:
type Optionalize<T> =
(RequiredKeys<T> extends never ? {} : { [K in RequiredKeys<T>]: T[K] })
& { [K in NonRequiredKeys<T>]?: T[K]; };
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
提供されたPlaygroundリンクから始めて、Test4とTest5の違いを再現してください。空のmapped typeがoptionalおよびrequiredのオブジェクト型と交差したときに、コンパイラーがどのように単純化するかを追跡してください。Test4がfalseと評価され、その他の報告された期待値が変更されない状態になれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100