I want to define a function which takes a callback as a parameter, and that callback's parameters should be required. Typescript correctly reports a callback with a mismatched parameter type but says nothing about callbacks with no expected arguments.
Why does the second on call not error, and is there any way to make it error?
function on(callback: (num: number) => void) {
callback(5);
}
on((string:bob) => { // typescript error
console.log("What");
});
on(() => { // no typescript error?
console.log("What");
});
There isn't a way to do this.
Callbacks with fewer parameters than the caller provides are extremely common in JavaScript - functions like forEach, map, filter, etc all provide 3 or more arguments but are frequently given 1-parameter functions as callbacks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With