将typescript类型从函数提取到对象
尝试从给定函数类型生成类型。我想直接从函数中获取返回类型,并映射到如下所示对象的键。有将typescript类型从函数提取到对象,typescript,Typescript,尝试从给定函数类型生成类型。我想直接从函数中获取返回类型,并映射到如下所示对象的键。有ReturnType我试过了,但它不接受减缩器结构 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 }, }
ReturnType
我试过了,但它不接受减缩器
结构
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,
}
}
我不确定您想要什么,但以下是如何在以下情况下使用
ReturnType
:
类型项目={
[K减速器键]:返回式
}
…或更通用的版本:
interface ReducerDict {
[key: string]: (...args: any[]) => any
}
type Project<T extends ReducerDict> = {
[K in keyof T]: ReturnType<T[K]>
}
接口简化{
[key:string]:(…args:any[])=>any
}
项目类型={
[K in keyof T]:返回类型
}
我不确定您想要什么,但以下是如何在以下情况下使用ReturnType
:
类型项目={
[K减速器键]:返回式
}
…或更通用的版本:
interface ReducerDict {
[key: string]: (...args: any[]) => any
}
type Project<T extends ReducerDict> = {
[K in keyof T]: ReturnType<T[K]>
}
接口简化{
[key:string]:(…args:any[])=>any
}
项目类型={
[K in keyof T]:返回类型
}
下面显示的对象在哪里?@Paleor抱歉没有找到。刚刚添加,看看是否有意义。下面显示的对象在哪里?@Paleor抱歉错过了。刚刚添加的,看看它是否有意义。这似乎是工作,但我需要更多的通用。我可以做type项目={[K in keyof T]:ReturnType}代码>@user1595858我编辑过。是的,你可以,但是T
必须局限于函数字典。这似乎可行,但我需要更通用的。我可以做type项目={[K in keyof T]:ReturnType}代码>@user1595858我编辑过。可以,但是T
必须限于函数字典。