Bind is Bad
Last updated
Was this helpful?
Last updated
Was this helpful?
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 with an explicit type annotation:
But if you expect a curried function .
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
:
Another alternative is to manually specify the type of the variable you are binding e.g.
If you have a class member function that you expect to pass around, e.g one would write the same Adder
class as: