Improve 'bind' typing in --strictBindCallApply mode #28920
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
With this PR we improve typing of the single-parameter
bind
method in--strictBindCallApply
mode such that generic functions and overloaded functions are more accurately typed (i.e. we improve typing of calls tobind
that bind justthis
and none of the function's parameters). Previously,bind
always erased type parameters and propagated only the last overload signature. Now, when calling the single-parameterbind
method on a function that has no explicitly declaredthis
parameter (which turns out to be >99% of all observed uses ofbind
), we simply propagate the function type itself, thus preserving generics and overloads.The PR introduces two new conditional types in
lib.d.ts
:ThisParameterType<T>
: Extracts the type of thethis
parameter ofT
, or 'unknown' ifT
has no 'this' parameter.OmitThisParameter<T>
: Removes the 'this' parameter fromT
. IfT
has no explicitly declaredthis
parameter, the result is simplyT
. Otherwise, a new function type with nothis
parameter is created fromT
. Generics are erased and only the last overload signature is propagated in this new function type.Fixes #28582.
Fixes #28900.