microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
101.01k stars 12.48k forks source link

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

Open ozyman42 opened 3 years ago

ozyman42 commented 3 years ago

Bug Report

🔎 Search Terms

🕗 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.

RyanCavanaugh commented 3 years ago

Shorter repro. It's not obvious to me which of these is the "correct" answer given the definitions, but I agree a difference is unexpected.

type X<T, U> = { traits: T & U };
// XX: true
type XX = X<true, true> extends X<any, false> ? false : true;

// Same as above, but with & {} at the end
type Y<T, U> = { traits: T & U } & {};
// YY: false
type YY = Y<true, true> extends Y<any, false> ? false : true;
weswigham commented 3 years ago

Variance measurement. We only do alias symbol variance measurement for object and conditional types. So X has variance measurement applied, while Y does not. The variance is being measured as both parameters being strictly Covariant, however any in an intersection mucks it up (by acting like both the bottom and top type) - {traits: false & any} is {traits: any}, which {traits: true} definitely extends, hence the structural result in YY.

ozyman42 commented 3 years ago

I'm curious, why is false & any equal to any in the first place? I had figured any is the set of all types and so the intersection of any and false should be false.

weswigham commented 3 years ago

You want unknown - any is actually the "disable the type checker for this element, it's untyped, so taint anything it touches as also untyped" type.