Debounce & Throttle Implementation in TypeScript
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Debounce & Throttle runs in O(1) time and O(1) space, in TypeScript as in every other language.
- ✓The same setTimeout/clearTimeout pair, but the timer handle is the interesting part: in the DOM lib setTimeout returns number, and under @types/node it returns NodeJS.Timeout.
- ✓Constrain the generic to the function type and return the same type, so the debounced value stays assignable wherever the original was.
- ✓The TypeScript trap: Typing the handle as number is the single most common TypeScript failure in this code, and it only breaks when the same file is later imported by a Node test runner or a server component, the error arrives far from the code that caused it.
- ✓Types cost nothing at runtime; this is the JavaScript implementation with the same 4ms clamp and the same background-tab throttling.
What Debounce & Throttle actually is
Debounce & Throttle in TypeScript: what you actually reach for
TypeScript implementation
type AnyVoidFn = (...args: never[]) => void;
type Debounced<T extends AnyVoidFn> = T & { cancel: () => void };
export function debounce<T extends AnyVoidFn>(fn: T, wait: number): Debounced<T> {
// ReturnType<typeof setTimeout>, NOT number: resolves to NodeJS.Timeout
// under @types/node and to number in the DOM lib.
let timer: ReturnType<typeof setTimeout> | undefined;
const debounced = ((...args: Parameters<T>) => {
if (timer !== undefined) clearTimeout(timer);
timer = setTimeout(() => fn(...(args as never[])), wait);
}) as Debounced<T>;
debounced.cancel = () => {
if (timer !== undefined) clearTimeout(timer);
timer = undefined;
};
return debounced;
}Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe TypeScript-specific trap
Performance in TypeScript, beyond the O(1)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which TypeScript type or library should I use for Debounce & Throttle?
The same setTimeout/clearTimeout pair, but the timer handle is the interesting part: in the DOM lib setTimeout returns number, and under @types/node it returns NodeJS.Timeout. Code that must compile in both targets should type the handle as ReturnType<typeof setTimeout>, which resolves correctly in either environment.
What is the most common TypeScript mistake when implementing Debounce & Throttle?
Typing the handle as number is the single most common TypeScript failure in this code, and it only breaks when the same file is later imported by a Node test runner or a server component, the error arrives far from the code that caused it. The other trap is typing the wrapper as (...args: any[]) => void, which erases the parameter types of everything it wraps.
How does Debounce & Throttle actually perform in TypeScript?
Types cost nothing at runtime; this is the JavaScript implementation with the same 4ms clamp and the same background-tab throttling. The only compile-time consideration is that a heavily generic wrapper can slow type-checking in large codebases if it is instantiated at hundreds of call sites. Time complexity is O(1) and space is O(1).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialIf you are a student, see what Replit offers students for free on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.