尝试从给定的函数类型生成类型。我想直接从函数中获取返回类型并映射到如下所示的对象的键。我尝试过ReturnType
,但是它没有接受reducers
结构。
type A = { a: string }
type B = { b: string }
type Reducers = {
aA: (a) => A,
bB:(b) => B,
}
const reducers: Reducers = {
aA: (a) => { a },
bB: (b) => {b },
}
如何获得类似的存储状态
namespace Store { // I will provide this
type Project = { // here is where I need to generated types based on reducer function like`type Project = ....`
aA: A,
bB: B,
}
}
答案 0 :(得分:2)
我不确定您想要什么,但是这里是在mapped type中使用ReturnType
的方法:
type Project = {
[K in keyof Reducers]: ReturnType<Reducers[K]>
}
…或更通用的版本:
interface ReducerDict {
[key: string]: (...args: any[]) => any
}
type Project<T extends ReducerDict> = {
[K in keyof T]: ReturnType<T[K]>
}