我想用 Ramda 将已知参数绑定到回调,其余部分未指定:
import * as R from "ramda";
type Params = Partial<{
param1: number;
param2: number;
param3: number;
}>;
function getBoundCallback(params: Params) {
const { param1, param2, param3 } = params;
const callback = (p1: number, p2: number, p3: number) => {
// do something
};
return R.curry(callback)(
param1 || R.__,
param2 || R.__,
param3 || R.__
);
}
但我得到:
TS2769: No overload matches this call.
The last overload gave the following error.
Argument of type 'number | Placeholder' is not assignable to parameter of type 'number'.
Type 'Placeholder' is not assignable to type 'number'
这样做的正确方法是什么?
我能想到的唯一解决方案是:
const callback = (
_p1: number | R.Placeholder,
_p2: number | R.Placeholder,
_p3: number | R.Placeholder
) => {
const p1 = _p1 as number;
const p2 = _p2 as number;
const p3 = _p3 as number;
// do something
};
这远非最佳。
这样做的正确方法是什么?
我"types/npm-ramda#dist"
用于 Ramda 类型声明。