我想创建一个函数对象(所有函数都有1个参数)。并创建另一个函数,该函数可以对参数从外部调用传递到对象中的一个函数进行类型保护。
const double = (v: number) => v * v
const concat = (v: string) => v + v
const functions = {
double, concat
}
const execute = <T extends keyof typeof functions>
(key: T, param: Parameters<typeof functions[T]>[0]) => {
functions[key](param) // here i can't match param type to function argument type, and getting an error
}
execute('double', 'str') // here everything is fine i get correct TypeError
如何解决这个问题?
蝴蝶刀刀
相关分类