i am having some troubles with array reduce in Typescript. To simplify, let's say I have a simple array of numbers where I want to remove duplicates and return a new array without them I was used to do something like this using reduce:
const new = nums.reduce((acc, item) => acc.includes(item) ? acc : [...acc, item], [])
where: nums = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4] and new should be: new = [0, 1, 2, 3, 4]
I have tried to type the function like this:
const new: number[] = nums.reduce((acc: number[], item:number) => acc.includes(item) ? acc : [...acc, item], [])
I receive the following error for "new":
TS2322: Type 'number' is not assignable to type 'number[]'.
and an error on the accumulator:
TS2769: No overload matches this call.
Seems like there is no way of telling typescript that the accumulator should be an array of numbers, any solution?
const new: number[] = nums.reduce((acc: number[], item:number) => acc.includes(item) ? acc : [...acc, item], new Array<number>());