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
src/a.ts:2:2 - error TS2322: Type 'object' is not assignable to type 'string[]'.
Property 'length' is missing in type '{}'.
2 return arr.reduce((acc, obj) => {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 acc.push(obj.toString());
~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 return acc;
~~~~~~~~~~~~~
5 }, [] as string[]);
~~~~~~~~~~~~~~~~~~~~
src/a.ts:3:7 - error TS2339: Property 'push' does not exist on type 'object'.
3 acc.push(obj.toString());
No error if arr is ReadonlyArray<number> or some other non-object type.
No error if I explicitly specify arr.reduce<string[]>.
No error if I remove the first two overloads to reduce, which are non-generic.
Error: Argument of type '(prevVal: Things, curVal: Things) => number' is not assignable to parameter of type '(previousValue: Things, currentValue: Things, currentIndex: number, array: Things[]) => Things'.
Expected result is no error. We should be allowed to reduce an object to a single value that isn't the same object without using an initializer.
You're not passing an intial value into .reduce(), so JavaScript expects both prevVal and curVal to be Things. But you're returning a number from the callback, which goes into prevVal.
TypeScript says that you have => number but it should be => Things. This is a legit, helpful error message.
I have an array with zeroes and ones created from a comparison (the example I'm posting here is a stupid version of it, just so the array type is (0 | 1)[]).
The reduce result is a positive number, but TS infers it will be 0 | 1.
const arr = [0, 1, 0, 0, 1, 1, 1, 1, 1, 1].map(v => v === 0 ? 0 : 1)
const sum = arr.reduce((p, c) => p + c, 0)
In this code reducedArray and the map parameter inside the reduce function are being inferred of type Record<string, any> instead of Map<string, string>. This also happens if the accumulator is an array or a Set and probably with many other types as well, but it works with simple types like string or number.
If recordArray is a Record<K, T> where T isn't any (even if it's unknown) the reduce seems to correctly infer its return type and the type of the accumulator.
The example above is a pretty stupid example but I think it shows the issue pretty well. My real code looks more like the following:
In this example the expected types for map and acc is Map<number, any> but they are inferred as Record<string, any> instead.
Again the interesting part is that it seems to work as long as T isn't any in Record<K, T> and you can argue that the fix is to don't use any but still, it's a bug
But I agree that this should be considered a bug and fixed! When an initial value is passed into reduce, the accumulator in the callback (and thus the return type of reduce) should not be inferred from the source array.
Activity
willyboy commentedon Mar 28, 2021
I think this goes here:
Error:
Argument of type '(prevVal: Things, curVal: Things) => number' is not assignable to parameter of type '(previousValue: Things, currentValue: Things, currentIndex: number, array: Things[]) => Things'.
Expected result is no error. We should be allowed to reduce an object to a single value that isn't the same object without using an initializer.
lolmaus commentedon Mar 28, 2021
William Neely (@willyboy) It's indicating a bug in your code.
You're not passing an intial value into
.reduce(), so JavaScript expects bothprevValandcurValto beThings. But you're returning a number from the callback, which goes intoprevVal.TypeScript says that you have
=> numberbut it should be=> Things. This is a legit, helpful error message.Check out this snippet for comparison.
russoedu commentedon Jun 8, 2022
I can add something to this.
I have an array with
zeroesandonescreated from a comparison (the example I'm posting here is a stupid version of it, just so the array type is (0 | 1)[]).The
reduceresult is a positive number, but TS infers it will be 0 | 1.Vulcagon commentedon Mar 22, 2025
I have a similar issue:
In this code
reducedArrayand themapparameter inside thereducefunction are being inferred of typeRecord<string, any>instead ofMap<string, string>. This also happens if the accumulator is an array or aSetand probably with many other types as well, but it works with simple types likestringornumber.If
recordArrayis aRecord<K, T>whereTisn'tany(even if it'sunknown) thereduceseems to correctly infer its return type and the type of the accumulator.The example above is a pretty stupid example but I think it shows the issue pretty well. My real code looks more like the following:
Edit: The example below is actually a simple and useful example. It converts a
Record<string, any>[]into aMap<number, any>:In this example the expected types for
mapandaccisMap<number, any>but they are inferred asRecord<string, any>instead.Again the interesting part is that it seems to work as long as
Tisn'tanyinRecord<K, T>and you can argue that the fix is to don't useanybut still, it's a buglolmaus commentedon Mar 24, 2025
Vulcagon (@Vulcagon) You can work around this problem by typing the accumulator in the callback:
But I agree that this should be considered a bug and fixed! When an initial value is passed into
reduce, the accumulator in the callback (and thus the return type ofreduce) should not be inferred from the source array.