【发布时间】:2021-04-02 08:37:50
【问题描述】:
在下面的示例代码中:
const obj = {
a: () => 1,
b: () => '2'
}
type typeMap = Record<keyof typeof obj, (arg: unknown) => void>
const map: typeMap = {
a: (numberArg) => numberArg + 1,
b: (stringArg) => stringArg.length
}
我希望 Typescript 知道 map 对象中的函数参数类型与原始 obj 对象中每个键的返回类型相同。我知道有 ReturnType<> 实用程序,但我不知道如何使用它来映射来自 obj 的每个道具。
总之,我希望numberArg 的类型为number,stringArg 的类型为string。
您可以在Typescript Playground打开此代码。
【问题讨论】:
标签: typescript