【发布时间】:2021-02-10 04:30:49
【问题描述】:
我希望高阶函数能够捕获传递函数的签名参数,该函数可以具有不同的签名。
我不知道这是否可行,但这是我的方法:
type FuncA = (a: string, b: number) => void
type FuncB = (a: string) => void
type Func = FuncA | FuncB
const a: FuncA = (a: string, b: number) => {
console.log('FuncA')
}
const b: FuncB = (a: string) => {
console.log('FuncB')
}
// My higher order function
const c = (func: Func) => {
// do something here...
return (...args: Parameters<typeof func>) => {
func(...args) // Expected 2 arguments, but got 0 or more. ts(2556). An argument for 'a' was not provided.
}
}
我的高阶函数c无法传递func的参数
似乎 TypeScript 无法区分 Func 类型的不同可能签名。
有人知道编写这种代码的模式吗?
谢谢!
【问题讨论】:
标签: node.js typescript typescript-typings