Merged in chore/typing-debounce (pull request #1668)

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

Approved-by: Simon.Emanuelsson
This commit is contained in:
Christian Andolf
2025-03-31 08:15:00 +00:00

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)
}