我正在尝试键入一个辅助函数,该辅助函数接受一个函数的对象并将其包装在另一个fn中;
const bindFn = (obj, wrapFn) => Object.entries(obj).reduce((carry, [key, fn])=>{
carry[key] = ( ...args ) => wrapFn(fn.apply(null, args))
return carry;
},{})
// example of usage
const n = bindFn( { sum: (x) => x+1 }, console.log);
n.sum(3); // this should console.log 4
问题是我不知道如何键入bindFn返回正确的类型,该类型包含具有相同键的对象并返回提供的对象的类型。像
interface BindFn = {
[name: keysof obj] : () => Returntype<obj[name]> :D !!! no clue.
}
答案 0 :(得分:0)
让我知道这是否是您想要的吗?认为这是对的。
const bindFn = <A extends keyof any, B>(obj: Record<A, B>, wrapFn: (fn: (arg: B) => B) => any): typeof obj => Object.entries(obj).reduce((carry, [key, fn])=>{
carry[key] = ( ...args: any[] ) => wrapFn(fn.apply(null, args))
return carry;
}, {})
const obj = { name: () => "my name is", age: () => "my age is" };
const result = bindFn(obj, (arg) => {
console.log("executed on");
return arg;
})
或执行的功能,版本
const bindFn = <A extends keyof any, B , C>(obj: Record<A, B>, wrapFn: (fn: (arg: B) => any) => C): Record<A, C> => Object.entries(obj).reduce((carry, [key, fn])=>{
carry[key] = ( ...args: any[] ) => wrapFn(fn.apply(null, args))
return carry;
}, {})
const obj = { name: () => "my name is", age: () => "my age is" };
const result = bindFn(obj, (arg) => {
console.log("executed on");
return 5;
}) // {name: number, age: number}
答案 1 :(得分:0)
尝试一下:
const bindFn = <T extends {[key: string]: Function}>(
obj: T,
wrapFn: Function
) => {
return Object.entries(obj).reduce((carry, [key, fn]) => {
carry[key] = (...args) => wrapFn(fn.apply(null, args));
return carry;
}, {}) as T;
};
const n = bindFn({ sum: (x: number) => x + 1 }, console.log);
n.sum(3);
n.sum('3'); // error: type string does not match number
n.foo(2); // error: 'foo' does not exist on the first arg of bindFn
const z = bindFn('foo', console.log); // error: 'foo' is not the right shape
这样,您将获得有用的类型检查,因为bindFn()
的返回类型将始终与第一个参数相同。例如n.sum()
的类型为(x: number) => x + 1
,因为这就是传递给bindFn()
的原因(因为return ... as T
)
这还将强制要求bindFn()
的第一个参数的形状必须是带有作为功能键的对象(因为T extends {[key: string]: Function}
)