Last active
April 21, 2022 13:48
-
-
Save ottokruse/f37e9ba9a8c8b669ed8e4e9877c84c05 to your computer and use it in GitHub Desktop.
TypeScript debounce that keeps argument types of debounced function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function debounce<T extends (...args: any[]) => void>( | |
func: T, | |
timeout = 300 | |
) { | |
let timer: ReturnType<typeof setTimeout>; | |
return (...args: Parameters<T>) => { | |
if (timer) clearTimeout(timer); | |
timer = setTimeout(() => func(...args), timeout); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment