Back to BuildfyTemplate PreviewUse Template
Build a REST API with auth
Hello! I'm your AI coding assistant. I can help you write code, debug issues, explain concepts, and more. What would you like to build today?
10:24 AM
Can you show me how to create a debounce function in TypeScript?
10:25 AM
Sure! Here's a type-safe debounce implementation:
typescript
function debounce<T extends (...args: unknown[]) => void>(
  fn: T,
  delay: number
): (...args: Parameters<T>) => void {
  let timer: ReturnType<typeof setTimeout>
  return (...args: Parameters<T>) => {
    clearTimeout(timer)
    timer = setTimeout(() => fn(...args), delay)
  }
}

// Usage:
const handleSearch = debounce((query: string) => {
  console.log('Searching for:', query)
}, 300)
This generic version preserves the parameter types of the original function, giving you full type safety.
10:25 AM

Press Enter to send ยท Shift+Enter for new line