microsoft / microsoft/TypeScript
Conditional type which checks nested intersect types evaluates inconsistently when outer type is also intersected and inner type intersects with any.
@weswigham is already working on this.
Since Jan 19, 2021.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.