constrain thisArg: any
in Function.prototype
methods
#17581
Labels
Duplicate
An existing issue was already created
thisArg: any
in Function.prototype
methods
#17581
Currently
apply
,call
andbind
each feature athisArg
parameter typed asany
.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 onthisArg
would be if we were to rebind saidbar
method:Foo
. Unfortunately,bind
is defined on theFunction
interface, which has no notion ofFoo
.To improve on this, I would suggest parameterizing the
Function
interface to e.g.Function<T>
, where any methods onFoo
would automatically count asFunction<Foo>
such thatthisArg
forapply
,call
andbind
could then, through thisT
, be constrained toFoo
. Any function not defined on such an interface would then get theFunction.prototype
methods forFunction<any>
.Let's say instead of interface
Foo
we would have an interfaceBar<U extends Baz>
. In this instance, rather than binding its methods toFunction<Bar<U>>
, it would be preferable to bind them to something likeFunction<Bar<any>>
(erase the generics) /Function<Bar<Baz>>
. After all, methods on interfaceBar<U>
would support anyBar
, rather than its variants specific to any value ofU
.To prevent breaking changes, this new generic on
Function
would have to provide a default value ofany
to ensure existingFunction
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.
The text was updated successfully, but these errors were encountered: