Skip to content

Instantly share code, notes, and snippets.

@fr-ser
Last active July 15, 2023 15:12
Show Gist options
  • Save fr-ser/ded7690b245223094cd876069456ed6c to your computer and use it in GitHub Desktop.
Save fr-ser/ded7690b245223094cd876069456ed6c to your computer and use it in GitHub Desktop.
Typed debounce function wtih TypeScript
export function debounce<F extends Function>(func:F, wait:number):F {
let timeoutID:number;
if (!Number.isInteger(wait)) {
console.warn("Called debounce without a valid number")
wait = 300;
}
// conversion through any necessary as it wont satisfy criteria otherwise
return <any>function(this:any, ...args: any[]) {
clearTimeout(timeoutID);
const context = this;
timeoutID = window.setTimeout(function() {
func.apply(context, args);
}, wait);
};
};
@fr-ser
Copy link
Author

fr-ser commented Sep 6, 2019

The versions look good. The only thing that comes to mind is I use a spread operator (...) before this line. Might be that the typescript setting of the storybook webpack don't support this feature yet 🤷‍♂️

@cuongdevjs
Copy link

Good. Thanks a lot :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment