Bind is Bad
This is the definition of bind
in lib.d.ts
:
As you can see it returns any! That means that calling bind
on a function will cause you to completely lose any type safety of the original function signature.
For example the following compiles:
A better way to write it would be with a simple arrow function with an explicit type annotation:
But if you expect a curried function there is a better pattern for that.
Class Members
Another common use is to use bind
to ensure the correct value of this
when passing around class functions. Don't do that!
The following demonstrates the fact that you lose parameter type safety if you use bind
:
If you have a class member function that you expect to pass around, use an arrow function in the first place e.g one would write the same Adder
class as:
Another alternative is to manually specify the type of the variable you are binding e.g.
Last updated