I have a custom map function in TypeScript defined as follows:
function map(arr: number[], fn: (n: number, i: number) => number): number[] {
let returnedArray: number[] = [];
for (let index = 0; index < arr.length; index++) {
returnedArray.push(fn(arr[index], index));
}
return returnedArray;
}
This function expects the fn argument to be a function that takes two parameters: a number (n) representing the array element and a number (i) representing the index, and returns a number.
However, when I try to pass a function that only accepts one parameter, like this:
function plusone(n: number) {
return n + 1;
}
map([1, 2, 3], plusone);
I am not getting any TypeScript errors during compilation (using tsc from the command line).
Can anyone pls tell me why?
[1, 2, 3].map(x => x+1)all the time, even thoughmapcalls the callback with more arguments. You might want to see an error, but the overwhelming majority of cases where this happens do not want an error. It's not like there's a runtime problem, is there?