export const debounce = any>( fn: TFunc, options?: { wait: number; immediate?: boolean }, ): ((...args: Parameters) => void) => { const { wait, immediate = false } = options ?? {}; let prevResult: ReturnType | undefined; let timeout: number | null = null; return (...args: Parameters): void => { if (typeof timeout === "number") clearTimeout(timeout); timeout = setTimeout(() => { timeout = null; if (!immediate) prevResult = fn(args); }, wait) as number; if (immediate && !timeout) { fn(args); return; } return prevResult; }; };