Types no longer inferred from Array.reduce initial value [closed]

2 weeks ago 40
ARTICLE AD BOX

My code previously worked without issues:

const result = someArray.reduce((map, v) => { ... return map.set(someKey, someValue); }, new Map<string, number>());

I would see the type of result correctly as a Map.

But recently I changed some settings in my Deno JSON file and ran some cleanup and re-install and now I get a bunch of TypeScript errors. I assume it's because the TypeScript version was either upgraded or downgrade (I don't know where to check this).

One of the errors is that TypeScript does not see the correct type of result any more, I have to specifically pass it to the generic definition like this:

const result = someArray.reduce<Map<string, number>>((map, v) => { ... return map.set(someKey, someValue); }, new Map());

Was this a change in TypeScript across versions?

Read Entire Article