英文:
How do I make a generic type function to map the return types of each key?
问题
Here is the translation of the provided code snippet:
如果我有
```typescript
type T1 = {
a: () => string,
b: () => number
}
type T2 = {
[P in keyof T1]: ReturnType<T1[P]>
}
那么 T2 是
{
a: string,
b: number
}
我如何创建一个通用类型函数来映射返回类型,假设通用类型的每个键都是一个函数?
type MapReturnType<T extends Record<string, ???>> = {
[P in keyof T]: ReturnType<T[P]>
}
<details>
<summary>英文:</summary>
If I have
type T1 = {
a: () => string,
b: () => number
}
type T2 = {
[P in keyof T1]: ReturnType<T1[P]>
}
Then T2 is
{
a: string,
b: number
}
How do I make a generic type function to map the return types, assuming each key of the generic type is a function?
type MapReturnType<T extends Record<string, ???>> = {
[P in keyof T]: ReturnType<T[P]>
}
</details>
# 答案1
**得分**: 1
你可以使用 `() => any` 作为你拥有的值,因为它是一个函数。
```typescript
type MapReturnType<T extends Record<string, () => any>> = {
[P in keyof T]: ReturnType<T[P]>
}
英文:
You can use () => any
as the value you have is a function
type MapReturnType<T extends Record<string, () => any>> = {
[P in keyof T]: ReturnType<T[P]>
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论