Skip to content
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

constrain thisArg: any in Function.prototype methods #17581

Closed
KiaraGrouwstra opened this issue Aug 3, 2017 · 2 comments
Closed

constrain thisArg: any in Function.prototype methods #17581

KiaraGrouwstra opened this issue Aug 3, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@KiaraGrouwstra
Copy link
Contributor

Currently apply, call and bind each feature a thisArg parameter typed as any.
A comment by @amir-arad at gcanti/typelevel-ts#8 (comment) made me realize we might well be able to improve on that.

If we have a Foo interface with methods e.g. bar() on it, it should be clear what the constraint on thisArg would be if we were to rebind said bar method: Foo. Unfortunately, bind is defined on the Function interface, which has no notion of Foo.

To improve on this, I would suggest parameterizing the Function interface to e.g. Function<T>, where any methods on Foo would automatically count as Function<Foo> such that thisArg for apply, call and bind could then, through this T, be constrained to Foo. Any function not defined on such an interface would then get the Function.prototype methods for Function<any>.

Let's say instead of interface Foo we would have an interface Bar<U extends Baz>. In this instance, rather than binding its methods to Function<Bar<U>>, it would be preferable to bind them to something like Function<Bar<any>> (erase the generics) / Function<Bar<Baz>>. After all, methods on interface Bar<U> would support any Bar, rather than its variants specific to any value of U.

To prevent breaking changes, this new generic on Function would have to provide a default value of any to ensure existing Function references would not suddenly be met with a "wrong number of generics" error.

There is another discussion on these three methods at #212, though it is focused more so on improving inference for their return types, rather than on constraining their parameters.

@mhegazy
Copy link
Contributor

mhegazy commented Aug 23, 2017

Setting this types on interface/class methods is something we have dappled with a few times. you can read more about tha tin #10288 and #7689

@mhegazy mhegazy added the Duplicate An existing issue was already created label Aug 23, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Sep 7, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Sep 7, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants