typescript - 打字稿:每个键的映射对象值类型
问题描述
在下面的示例代码中:
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
成为 typenumber
并且stringArg
成为 type string
。
您可以在Typescript Playground中打开此代码。
解决方案
推荐阅读
- php - 将 DOM createElement 中的两个变量相乘
- php - 调用带有引用的函数时,foreach 循环的行为异常(将一项替换为另一项)
- apache-kafka - Kafka 处理的生产者-消费者处理模式
- jquery - 如何找到具有相同属性值的所有xml节点
- android - 当孩子的身高不同时,均匀对齐gridview
- react-native - 世博账号登录超时
- sql - 如何使用 contains 来查找包含空格的关键字?
- c# - 访问 viewModel 中的行为属性
- javascript - Azure:通过主体 ID 获取用户电子邮件
- javascript - 如何编辑/删除同位素库中的“全部显示”视图?