Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.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
Reactjs 如何在TypeScript接口中指定具有多个对象的数组?_Reactjs_Typescript_Redux - Fatal编程技术网

Reactjs 如何在TypeScript接口中指定具有多个对象的数组?

Reactjs 如何在TypeScript接口中指定具有多个对象的数组?,reactjs,typescript,redux,Reactjs,Typescript,Redux,我有一些React-Redux类型脚本代码,我正在从API获得结果,并尝试为响应对象创建接口。下面是我的示例数据: const exampleState: ExampleState = { loading: false, products: { "id": 11001, "campus": "hrnyc", "name": "Camo Onesie",

我有一些React-Redux类型脚本代码,我正在从API获得结果,并尝试为响应对象创建接口。下面是我的示例数据:

    const exampleState: ExampleState = {
  loading: false,
  products: {
    "id": 11001,
    "campus": "hrnyc",
    "name": "Camo Onesie",
    "slogan": "Blend in to your crowd",
    "description": "The So Fatigues will wake you up and fit you in. This high energy camo will have you blending in to even the wildest surroundings.",
    "category": "Jackets",
    "default_price": "140.00",
    "created_at": "2021-01-12T21:17:59.200Z",
    "updated_at": "2021-01-12T21:17:59.200Z",
    "features": [
        {
            "feature": "Fabric",
            "value": "Canvas"
        },
        {
            "feature": "Buttons",
            "value": "Brass"
        }
    ]
  }
};
这是我对这个对象的接口:

export interface Featured {
  "id": number
  "campus": string,
  "name": string,
  "slogan": string,
  "description": string,
  "category": string,
  "default_price": string,
  "created_at": string,
  "updated_at": string,
  "features": [
      {
          "feature": string,
          "value": string
      }
  ]
}
我的减速器中出现以下错误:

TypeScript error in /Users/theo/Desktop/hrnyc34-fec-falcullele/my-app/src/reducers/singleProductReducer.ts(20,5):
Type '[{ feature: string; value: string; }, { feature: string; value: string; }]' is not assignable to type '[{ feature: string; value: string; }]'.
  Types of property 'length' are incompatible.
    Type '2' is not assignable to type '1'.  TS2322

    18 |     "created_at": "2021-01-12T21:17:59.200Z",
    19 |     "updated_at": "2021-01-12T21:17:59.200Z",
  > 20 |     "features": [
       |     ^
    21 |         {
    22 |             "feature": "Fabric",
    23 |             "value": "Canvas"

据我所知,问题在于“features”数组中有多个对象。此功能阵列中可能有多达12个对象。如何纠正此typescript错误?

功能指定的类型是一个固定长度数组,其元素具有已知类型。以下是您想要的:

type FeatureType = {
   "feature": string,
   "value": string
}

export interface Featured {
  "id": number
  "campus": string,
  "name": string,
  "slogan": string,
  "description": string,
  "category": string,
  "default_price": string,
  "created_at": string,
  "updated_at": string,
  "features": FeatureType[]
}

(注意:为了可读性,我将'feature'提取到类型别名中,您不必这样做)

您指定了元组类型,而不是数组类型。看@jornsharpe你完全正确。目前正在动态学习typescript。谢谢你的帮助。如果你正在提取特征类型,我仍然会在特征中保留数组部分,那么你可以轻松访问该数组中每个值的类型。这是个好主意,我将编辑答案。