Here is a code snippet that I am working on:
function reverse<T extends Number, D extends Number>(items: T[], m: D): T[] {
var toreturn = [];
for (var i = items.length - 1; i >= 0; i--) {
(()=>{
toreturn.push(items[i] * m);
})();
}
return toreturn;
}
var sample = [1, 2, 3];
var reversed = reverse(sample, 10);
console.log(reversed);
My development environment has flagged 2 errors in this code:
Error:(5, 27) TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.
Error:(5, 38) TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.
The issue stems from the fact that the entities being multiplied are not recognized as numbers or other valid types. I attempted to resolve this by specifying extends in the generic definition.
How can this issue be resolved?
You can find the relevant TypeScript Playground version here