chore: debounce function is now typed to accommodate typed parameters in a callback
This commit is contained in:
@@ -1,9 +1,11 @@
|
|||||||
export function debounce(func: Function, delay = 300) {
|
export function debounce<Params extends any[]>(
|
||||||
|
func: (...args: Params) => any,
|
||||||
|
delay = 300
|
||||||
|
) {
|
||||||
let debounceTimer: ReturnType<typeof setTimeout>
|
let debounceTimer: ReturnType<typeof setTimeout>
|
||||||
return function () {
|
|
||||||
// @ts-expect-error this in TypeScript
|
return function <U>(this: U, ...args: Parameters<typeof func>) {
|
||||||
const context = this
|
const context = this
|
||||||
const args = arguments
|
|
||||||
clearTimeout(debounceTimer)
|
clearTimeout(debounceTimer)
|
||||||
debounceTimer = setTimeout(() => func.apply(context, args), delay)
|
debounceTimer = setTimeout(() => func.apply(context, args), delay)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user