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
Can't call forEach on number[] | ReadonlyArray<never> #16716
In that issue, Jakub Korzeniowski (@kujon) was asking that A[] | B[] should reduce to (A|B)[] which is consistent with the way TypeScript currently treats arrays as covariant in their content type. But possibly-mutable arrays should be invariant in their type, so maybe that fix would be going farther in the wrong direction.
The other way of looking at this issue is:
typeOverloaded<A,B>={(a: A): void;(b: B): void;}functionexploded<A,B>(f: Overloaded<A,B>,ab: A|B): void{f(ab);// error!varisA: (ab)=>ab is A;isA(ab) ? f(ab) : f(ab);// workaround, no error }
TypeScript can't take an overloaded or generic function and realize that if it can resolve a parameter of type A and separately resolve a parameter of type B, then it "should" be able to handle a parameter of type A|B. But here be dragons.
Activity
jcalz commentedon Jun 23, 2017
I think the first error message is from the same issue reported in #16644:
In that issue, Jakub Korzeniowski (@kujon) was asking that
A[] | B[]should reduce to(A|B)[]which is consistent with the way TypeScript currently treats arrays as covariant in their content type. But possibly-mutable arrays should be invariant in their type, so maybe that fix would be going farther in the wrong direction.The other way of looking at this issue is:
TypeScript can't take an overloaded or generic function and realize that if it can resolve a parameter of type
Aand separately resolve a parameter of typeB, then it "should" be able to handle a parameter of typeA|B. But here be dragons.