Improve 'bind' typing in --strictBindCallApply mode by ahejlsberg · Pull Request #28920 · microsoft/TypeScript (original) (raw)
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 to bind
that bind just this
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-parameter bind
method on a function that has no explicitly declared this
parameter (which turns out to be >99% of all observed uses of bind
), 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.