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:
functiontwoParams(a:number,b:number){return a + b;}let curryOne =twoParams.bind(null,123);curryOne(456);// Okay but is not type checked!curryOne('456');// Allowed because it wasn't type checked!
A better way to write it would be with a simple arrow function with an explicit type annotation:
functiontwoParams(a:number,b:number){return a + b;}letcurryOne=(x:number)=>twoParams(123,x);curryOne(456);// Okay and type checked!curryOne('456');// Error!
class Adder {
constructor(public a: string) { }
add(b: string): string {
return this.a + b;
}
}
function useAdd(add: (x: number) => number) {
return add(456);
}
let adder = new Adder('mary had a little 🐑');
useAdd(adder.add.bind(adder)); // No compile error!
useAdd((x) => adder.add(x)); // Error: number is not assignable to string
class Adder {
constructor(public a: string) { }
// This function is now safe to pass around
add = (b: string): string => {
return this.a + b;
}
}