-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Proposal: Overload Function.bind for when no argArray is provided. #22669
Comments
I'm in favour, I have been adding similar interface augmentation into my projects for ages. I have to point out though that since FWIW, with TypeScript 2.8, it is possible to construct a proper type for P.S. I should note, it'd be possible to avoid horribleness above if we had variadic generic types. |
Echoing @lierdakil: A top-level function would give you far better safety in this regard: function getBound<T extends {[K1 in K]: Function}, K extends keyof T>(obj: T, member: K): T[K] {
return obj[member].bind(obj);
} There's no good way to declare the |
I would love to get this implemented, but the automated PR checks said unless this issue is signed off and added to the next version's requested changes, my PR wouldn't get merged. This is particularly annoying when leveraging Storybook's CSF syntax as they recommend using @RyanCavanaugh let me know if this is something I can work on. |
Background
Currently the
Function.bind
type definition defines only one signature:This destroys typing information on the bound function. Given difficulties in generating a new type for the bound function (the number of arguments has changed) this makes sense. However, it's very common to bind functions without using the
argArray
argument tobind
. In this case a better solution can be made.Proposal
I propose that
bind
be overloaded with a second signature:Example
To use a real world example on how this improves TypeScript I'll pull something from a personal project.
Types used:
Under the current
bind
signature a change togameGuess
such that it accepts a different type would produce no error:But with
bind
utilizing a special definition for the case of only using thethisArg
argument - the above example would produce an error.The text was updated successfully, but these errors were encountered: