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
The text was updated successfully, but these errors were encountered:
vp2177
changed the title
Array.prototype.reduce return type incorrectly inferred as 'any'
Array.prototype.reduce return type incorrectly inferred as 'any' when Array has type any[]Oct 20, 2019
The overloads of reduce() are in a suboptimal order when the array element type is very wide like any or unknown (if the initial value is assignable to it). If the generic call signature were moved to the front, this issue would not arise:
I'm not sure if this rises to the level of "bug"; in any case, you could always locally merge in the above declarations in your code base to resolve it (actually you only need the generic one).
TypeScript Version: 3.6.3
Search Terms: Array reduce type inferred bad|incorrect|any
Code
Expected behavior:
x
should be inferred to be of typestring
Actual behavior:
x
is inferred to beany
However,
x
is is correctly inferred to be typestring
, if using[].reduce(...
as opposed tos.reduce(...
.Compare:
with
Inferring
x
's type even ifs
is polymorphic is possible and works in other languages:Playground Link:
Related Issues:
#21061
The text was updated successfully, but these errors were encountered: