const r1 = [1, 2, 3].myReduce((a, c) => a + c); // 6
const r2 = [1, 2, 3].myReduce((a, c, i, arr) => a + c, 10); // 16
const r3 = [{ num: 1 }, { num: 2 }, { num: 3 }].myReduce(
function(a, c) {
return { ...a, [c.num]: 'hi' };
},
{},
); // { 1: 'hi', 2: 'hi', 3: 'hi' }
// Argument of type '{}' is not assignable to parameter of type '{ num: number; }'. Property 'num' is missing in type '{}' but required in type '{ num: number; }'.
const r4 = [{ num: 1 }, { num: 2 }, { num: 3 }].myReduce(
function(a, c) {
return a + c.num;
},
'',
); // '123'
// Operator '+' cannot be applied to types '{ num: number; }' and 'number'.
// Argument of type 'string' is not assignable to parameter of type '{ num: number; }'.
interface Array<T> {
myReduce(callback: (a: T, c: T, i: number, arr: T[]) => T, iV?: T): T;
}