You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Regression: Intersection of array and tuple not assignable to tuple #38348
Presumably, this is related to the breaking change in 3.9 around handling of tuple types more strictly. I believe the code is correct however and should compile, because the array type number[] is a strict supertype of the tuple type [number, ...number[]] (surely?) - perhaps the special case unifying tuple types with matching element types to an array type hasn't been implemented?
I believe that this is because we no longer just consider intersections to be assignable if any constituent is assignable - the type has to be compatible as a whole. Anders Hejlsberg (@ahejlsberg) made that change at #37195
Is there a specific reason you had a number[] & [number, ...number[]]?
The issue here is that we lack the ability to relate a tuple type with a rest element on the target side to anything but another tuple type on the source side. In the extra check introduced by #37195, the source type is an intersection, and so the check fails. I think an easy and reasonable fix is to omit the extra check when the target is a tuple type with a rest element.
I can see it's correct that number[] won't assign to [number, ...number[]]. But the intersection checking logic should realise that the two types being interescted are not unrelated, but that the tuple is a refinement of the array.
Even though { x: number } can't be assigned to { x : 1 } the checker doesn't do the thing where it requires both parts of the intersection type to be assignable to the target. So the logic is there correctly for objects; the case I ran into is the corresponding handling for an array+tuple intersection.
Is there a specific reason you had a number[] & [number, ...number[]]?
Apologies, I didn't motivate this very well! The example was perhaps too minimal.
The full code looked more like this (reworked to a vaguely minimal example). I hope you can see what was being attempted. No apologies made for the code - it is what it is - and thinking some more I can see that the types could be improved, but it's less clear what's going on in the original code which is a bit more involved.
typeChecker<T>=(x: unknown)=>x is T;typeNonEmptyArray<T>=[T, ...T[]];functionbothChecker<T1,T2>(c1: Checker<T1>,c2: (x: T1)=>Checker<T2>): Checker<T1&T2>{return(x: unknown): x is T1&T2=>c1(x)&&c2(x)(x);}constnumChecker: Checker<number>=(x: unknown): x is number=>typeofx==='number';functionarrayChecker<T>(elt: Checker<T>): Checker<T[]>{return(x: unknown): x is T[]=>Array.isArray(x)&&x.every(i=>elt(i));}functionnonEmptyChecker<T>(): Checker<NonEmptyArray<T>>{return(x: unknown): x is NonEmptyArray<T>=>(xasT[]).length>0;}consttheProblem: Checker<NonEmptyArray<number>>=bothChecker(arrayChecker(numChecker),_asArray=>nonEmptyChecker());
Activity
JasonGore commentedon May 5, 2020
Also applies to
3.9.1-rc.DanielRosenwasser commentedon May 5, 2020
I believe that this is because we no longer just consider intersections to be assignable if any constituent is assignable - the type has to be compatible as a whole. Anders Hejlsberg (@ahejlsberg) made that change at #37195
Is there a specific reason you had a
number[] & [number, ...number[]]?ahejlsberg commentedon May 6, 2020
The issue here is that we lack the ability to relate a tuple type with a rest element on the target side to anything but another tuple type on the source side. In the extra check introduced by #37195, the source type is an intersection, and so the check fails. I think an easy and reasonable fix is to omit the extra check when the target is a tuple type with a rest element.
NWilson commentedon May 6, 2020
I can see it's correct that
number[]won't assign to[number, ...number[]]. But the intersection checking logic should realise that the two types being interescted are not unrelated, but that the tuple is a refinement of the array.For example, this works in Nightly:
Even though
{ x: number }can't be assigned to{ x : 1 }the checker doesn't do the thing where it requires both parts of the intersection type to be assignable to the target. So the logic is there correctly for objects; the case I ran into is the corresponding handling for an array+tuple intersection.NWilson commentedon May 6, 2020
Apologies, I didn't motivate this very well! The example was perhaps too minimal.
The full code looked more like this (reworked to a vaguely minimal example). I hope you can see what was being attempted. No apologies made for the code - it is what it is - and thinking some more I can see that the types could be improved, but it's less clear what's going on in the original code which is a bit more involved.
NWilson commentedon May 8, 2020
Wow, thank you so much for a quick fix!