I'm trying to create a bound version of a function which has its arguments pre-set however I am unable to get any sort of type checks on the bind
method.
Here's my code:
interface I {
a: string;
b: string;
}
function doSomethingWithValue(value: I) {
// Do something
}
const ivalue: I = { a: 'a', b: 'b' };
// No error as expected.
doSomethingWithValue(ivalue);
//"Argument of type '"a"' is not assignable to parameter of type 'I'" as expected.
doSomethingWithValue('a');
// No error. Not what I expected
const bound = doSomethingWithValue.bind(null, 'a');
// Will fail
bound();
It seems that currently the TypeScript signature of bind
is
bind(this: Function, thisArg: any, ...argArray: any[]): any;
Is there any way I could get types checks to work correctly with bind
?
I've tried creating an index.d.ts
but I am stuck on how to declare function parameters as a generic.
There is compiler option in 3.2
and newer, called strictBindCallApply
documented here. You can also just enable strict
which also enables strictBindCallApply
With this option active you will get an error on this line:
const bound = doSomethingWithValue.bind(null, 'a');