在打字稿中,我们可以定义一个函数类型,如:
type FunctionHandler = {
(param1: string, param2: number): string
}
但我想声明一个泛型类型,它可以像我们在变量类型中所做的那样采用任意数量的参数及其类型:
type Variable<Props> = {
[p in keyof Props]: Props[p]
}
我想要类似的东西:
type FunctionHandler<Props> = {
([p in keyof Props]: Props[p]): void | boolean
}
但是我做不到,有人可以帮我吗?
答案 0 :(得分:0)
您可以像这样使用 rest param 来让函数接受任意数量的任何类型的参数:
type FunctionHandler = {
(...params: any): string
}
另见