They provide information in their documentation regarding this topic https://www.typescriptlang.org/docs/handbook/type-compatibility.html#comparing-two-functions
In essence, the reason for this behavior is that in JavaScript it is permissible and somewhat common to disregard extra function parameters. For instance, the code below declares a parameter b
, but never utilizes it:
const observer: Callback = function(a: string, b: string): void {
console.log(a);
}
You might agree that this should comply with the Callback
type definition. However, what role does b: string
play in the argument list? It only impacts the value of observer.length
and the return value of observer.toString()
. In practical terms, it often serves as redundant typing.
An example of this concept can be observed with higher-order functions like .map
, .filter
, .reduce
on arrays. These functions accept another function as an argument to define behavior. Despite receiving multiple parameters, you may only require one in many scenarios. Therefore, the following scenario should not generate a type error:
const numbers = [1, 2, 3];
const biggerNumbers = numbers.map(value => value + 1);
Even though your function receives a value, index, and array, it is acceptable to ignore the last two (or even all three, although less common).
In summary: The type definition enforces passing specific values into the function, rather than ensuring that the function actually utilizes those values.