chore: debounce function is now typed to accommodate typed parameters in a callback

This commit is contained in:
Christian Andolf
2025-03-28 14:40:36 +01:00
parent 7856d8c1f5
commit 32e769fd09

View File

@@ -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>
return function () {
// @ts-expect-error this in TypeScript
return function <U>(this: U, ...args: Parameters<typeof func>) {
const context = this
const args = arguments
clearTimeout(debounceTimer)
debounceTimer = setTimeout(() => func.apply(context, args), delay)
}