【发布时间】:2020-01-19 10:33:03
【问题描述】:
我在 TypeScript 中使用过 Parameters<T> 类型,它返回一个带有参数类型的元组。现在我想知道是否有一种方法不仅可以返回参数的类型,还可以返回名称?
类似:
function MyFunc(a: string, b: boolean, ...args: string[]) {}
type extractedType = ParametersWithName<typeof MyFunc>
extractedType 结果最终为:
{
a: string,
b: string
}
我会用它从常规函数参数转换为接收参数对象的函数(如 React 道具)。
【问题讨论】:
-
在 typescript 4 中
Parameters也将名称返回为Labeled Tuple Elements。但是我仍然不知道将其转换为对象的方法。keyof extractedType仍然不返回名称(但返回在这种情况下无关的数组道具)
标签: typescript