I am struggling with this TypeScript code that contains comments and seems a bit messy:
function getPlacesToStopExchange(): {
our: { i: number; val: number; }[];
enemy: { i: number; val: number; }[];
//[party in 'our' | 'enemy' ]: { i: number; val: number; }[];
} {
return valuesByMoves.reduce((o, v, i) => {
if (i === 0 || i % 2) {
// //we move
const el: { i: number; val: number; } = { i, val: v, };
o.enemy.push(el);
} else {
// o.our.push({ i, val: v, } as { i: number; val: number; }));
}
return o;
}, { enemy: [], our: [], });
}
Issue on line 10: o.enemy.push(el);
is causing an error:
error TS2345: Argument of type '{ i: number; val: number; }' is not assignable
to parameter of type 'never'.
134 o.enemy.push(el);
Commenting out the problematic line resolves the error. Thus, I believe the issue lies specifically there, rather than in the function's return value type or reduce initial value.
Can anyone provide a solution to this problem? Despite attempting various approaches, the error persists. Other answers on SO suggest specifying the type at the point of error, but my attempts have been unsuccessful thus far.
I have also tried exploring the tsc
code for more insights, but without any helpful call stack information. Any advice on debugging this tsc error would be greatly appreciated.
Thank you.