microsoft / microsoft/TypeScript

Conditional type which checks nested intersect types evaluates inconsistently when outer type is also intersected and inner type intersects with any.

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

@weswigham がすでに取り組んでいます。

2021年1月19日 から。

Needs Investigation Rescheduled
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

Bug Report

🔎 Search Terms
  • Conditional type
  • union
  • intersect
  • nested
🕗 Version & Regression Information

Confirmed that this issue appears in TypeScript versions 3.7, 4.1, 4.2

⏯ Playground Link

Playground link with relevant code

💻 Code
type IsNotA = { isA: false };
type IsA    = { isA: true };
type A      = IsNotA | IsA;

type IsNotB        = { isB: false };
type IsB           = { isB: true };
type B             = IsNotB | IsB;

type Container<a extends A, b extends B> = {
    traits: a & b;
};

declare const BContainer: Container<IsA, IsB>

type t1 = typeof BContainer['traits'] extends IsB          ? true  : false;
type t2 = typeof BContainer extends Container<any, IsB>    ? true  : false;
type t3 = typeof BContainer['traits'] extends IsNotB       ? false : true;
type t4 = typeof BContainer extends Container<any, IsNotB> ? false : true;

Notice that the types t1, t2, t3, t4 all evaluate to true. Right away it should raise alarms that t4 is evaluated to true because I thought that whenever we intersect a concrete type with any, it is still considered any by the TS compiler even if that behavior itself is an odd choice. Now things get stranger if we change the definition of Container to

type Container<a extends A, b extends B> = {
    traits: a & b;
} & {};

Notice that I intersected on a {} type at the end.

🙁 Actual behavior

t4 now evaluates to false. This definitely has something to do with the traits being a intersect type and the Container taking an any as type parameter, because if we replace the any with IsA or A when instantiating the Container in the extends clause then all evaluate to true again regardless of whether Container has an intersection with {} or not.

🙂 Expected behavior

Intersecting an empty object type {} with Container should not have changed the evaluation of the conditional type.
t4 should either evaluate to true or false in both scenarios. Probably false since it seems a concrete type intersected with any is still any. If TS worked as I would ideally have it then type thing = 0 & any would evaluate to 0 in which case t4 should be true in both cases actually, but TS says type thing = 0 & any is equivalent to any so t4 should always be false.

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

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

はじめの一歩

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

評価

この issue はまだ評価されていません。

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

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