export interface DebouncedFunction any> { (...args: Parameters): void; cancel: () => void; } export function debounce any>( func: F, wait: number ): DebouncedFunction { let timeout: ReturnType | null = null; const debounced = function (...args: Parameters) { if (timeout) { clearTimeout(timeout); } timeout = setTimeout(() => { func(...args); timeout = null; }, wait); }; debounced.cancel = () => { if (timeout) { clearTimeout(timeout); timeout = null; } }; return debounced as DebouncedFunction; }