Typed debounce in TypeScript
Debounce with the wrapped function's argument types preserved.
function debounce<A extends unknown[]>(
fn: (...args: A) => void,
wait: number,
): (...args: A) => void {
let timer: ReturnType<typeof setTimeout> | undefined;
return (...args: A) => {
clearTimeout(timer);
timer = setTimeout(() => fn(...args), wait);
};
}
Keywords and builtins used here
AReturnTypeextendsfunctionletnumberreturn
The run, in numbers
- Lines
- 10
- Characters to type
- 255
- Tokens
- 77
- Three-star pace
- 105 tpm
At the three-star pace of 105 tokens a minute, this run takes about 44 seconds.
Step 2 of 5 in Timing & memory, step 15 of 19 in Classes & patterns.