-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Infer the argument that the debounced function is expected to take #55
Comments
Hey @juanmendes! The change was not so easy unfortunately 😅 Since we're inferring also export declare function debounce<Args extends any[], F extends (...args: Args) => any>(func: F, waitMilliseconds?: number, options?: Options<ReturnType<F>>): {
(this: ThisParameterType<F>, ...args: Parameters<F> & Args): Promise<ReturnType<F>>;
cancel: (reason?: any) => void;
}; Your example correctly infers Thanks again for the idea, I've added you to the all-contributors list. |
For code like the following, the argument to the actual input handler is not inferred, it's
any
However, it is possible to infer the type based on what it's being assigned to. As in, assigned to the 2nd parameter for
addEventListener
as above, or if you did something like the following:The following is an args inferring version of a much simpler debounce than you have implemented. Please ignore the return type of
any
, which is just used to minimize noise and focus on inferring the arguments.The idea is that you don't need to declare the entire
(...args: any[]) => any;
as a generic, or aProcedure
as you have called it.By moving the generic part just to the arguments of the function (
<Args extends unknown[]>
), this magical inference occurs.Here's a stackblitz showing it in action
The text was updated successfully, but these errors were encountered: