I encountered an issue with my iteration.
The error message "Operator '+=' cannot be applied to types 'number' and 'T'" is showing up.
I am puzzled as to why this is happening.
let a: number = 1, b: number = 2, c: number = 3, d: number = 4;
function sumNumbers<T>(...numbers: T[]): T {
let total: number = 0;
for (let num of numbers) {
total += num;
}
return total;
}
console.log(a + ` + ` + b + ` = ` + sumNumbers<number>(a, b));
// 1 + 2 = 3
console.log(a + ` + ` + b + ` + ` + c + ` = ` + sumNumbers<number>(a, b, c));
// 1 + 2 + 3 = 6
console.log(a + ` + ` + b + ` + ` + c + ` + ` + d + ` = ` + sumNumbers<number>(a, b, c, d));
// 1 + 2 + 3 + 4 = 10
Thank you for your assistance.