【发布时间】:2020-09-23 15:24:42
【问题描述】:
我想创建一个函数对象(所有函数都有 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
如何解决?
【问题讨论】:
标签: javascript typescript types