Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将typescript类型从函数提取到对象_Typescript - Fatal编程技术网

将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
必须限于函数字典。