export function debounce<T extends (...args: any[]) => any>(
  fn: T,
  wait: number,
) {
  let timeout: ReturnType<typeof setTimeout> | null;
  return function (this: ThisParameterType<T>, ...args: Parameters<T>) {
    const context = this;
    const later = () => {
      timeout = null;
      fn.apply(context, args);
    };
    clearTimeout(timeout!);
    timeout = setTimeout(later, wait);
  };
}
