我想定义一个函数,作为参数获取另一个函数f
及其参数args
,我想让Typescript确保传递的参数对于传递的函数是正确的.
伪代码
function A (n: number, s: string, b: boolean) {
...
}
function B (f: Function, ...args: typeof arguments of f) {
...
f(args)
}
B(A, 1, 'str', true) // typescript is happy
B(A, 1, 'str') // typescript is sad
B(A, 1, undefined, true) // typescript is sad
// any other example of wrong arguments of A passed to b would raise Typescript error...
所以这里重要的部分是:
...args: typeof arguments of f
这显然是not个有效的打字脚本.
我怎样才能写出这样的打字脚本代码呢?