microsoft / microsoft/TypeScript

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

Đang mở
#42,369 4 bình luận 0 reaction 1 người được giao Xem trên GitHub

@weswigham đang làm issue này rồi.

Từ ngày 19/1/2021.

Needs Investigation Rescheduled
Ngôn ngữ chính
Go
Star
111k
Fork
14.3k
Merge trung bình
2 ngày 4 giờ
Pull request đã merge (30 ngày)
132

Mô tả

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.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.