I want to write a debounce function with typescript. My code is shown below:
function debounce(fn: Function, time: number): Function {
// ...
}
And then, my eslint tell me don't use Function as a type. Below is original text:
Don't use Function as a type. The Function type accepts any function-like value.
It provides no type safety when calling the function, which can be a common source of bugs.
It also accepts things like class declarations, which will throw at runtime as they will not be called with new.
If you are expecting the function to accept certain arguments, you should explicitly define the function shape.
That's the problem, any suggestion?
==================== Some update ==================================
If use javascript, my code will be:
function debounce(fn, time) {
let timer
return function (...args) {
clearTimeout(timer)
timer = setTimeout(() => fn(...args), time)
}
}