Javascript Lodash将curry与bind一起使用会导致键入脚本错误
我试图将Javascript Lodash将curry与bind一起使用会导致键入脚本错误,javascript,typescript,lodash,Javascript,Typescript,Lodash,我试图将bind方法与curry一起使用,但它给了我一个类型错误 const curried = curry(this.method.bind(this)); const getSomething = curried(a, b); 从getSomething获取TS错误: 应为0-1个参数,但得到2个 当我不使用bind方法时,它不会抱怨 const curried = curry(this.method); const getSomething = curried(a, b); 问题是,这
bind
方法与curry
一起使用,但它给了我一个类型错误
const curried = curry(this.method.bind(this));
const getSomething = curried(a, b);
从getSomething获取TS错误:
应为0-1个参数,但得到2个
当我不使用bind方法时,它不会抱怨
const curried = curry(this.method);
const getSomething = curried(a, b);
问题是,这是bind的签名:
bind(this: Function, thisArg: any, ...argArray: any[]): any;
因此,函数的bind
返回值为any
,curry
仍然有效,因为any
可以转换为任何其他类型,因此使用curry
声明顺序中的第一个重载,即此重载:
curry<T1, R>(func: (t1: T1) => R, arity?: number): CurriedFunction1<T1, R>;
现在,使用此版本的bind all应该可以正常工作(对于最多有5个参数的函数,但您可以轻松添加更多参数):
谢谢,一如既往的帮助。
function typedThisBind<T1, T2, T3, T4, R>(fn: (t: T1, t2: T2, t3: T3, t4 : T4) => R, thisArg: any) : typeof fn
function typedThisBind<T1, T2, T3, R>(fn: (t: T1, t2: T2, t3: T3) => R, thisArg: any) : typeof fn
function typedThisBind<T1, T2, R>(fn: (t: T1, t2: T2) => R, thisArg: any) : typeof fn
function typedThisBind<T1, R>(fn: (t: T1) => R, thisArg: any) : typeof fn
function typedThisBind<R>(fn: () => R, thisArg: any) : () => R
function typedThisBind(fn: Function, thisArg: any) : any {
return fn.bind(thisArg);
}
class Foo {
method(a: number, b: number) {}
m(){
const curried = curry(typedThisBind(this.method, this));
const getSomething = curried(0, 0); // Works as expected.
}
}