我正在尝试创建一个函数的绑定版本,该函数的参数已预先设置,但是我无法对该bind
方法进行任何类型的类型检查。
这是我的代码:
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();
目前的 TypeScript 签名似乎bind
是
bind(this: Function, thisArg: any, ...argArray: any[]): any;
有什么方法可以让类型检查正常工作bind
吗?
我试过创建一个,index.d.ts
但我坚持如何将函数参数声明为泛型。