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
Reactjs 定义适当的TypeScript接口_Reactjs_Typescript_Lodash - Fatal编程技术网

Reactjs 定义适当的TypeScript接口

Reactjs 定义适当的TypeScript接口,reactjs,typescript,lodash,Reactjs,Typescript,Lodash,我不熟悉打字脚本,遇到了一个问题。我正在构建一个React web应用程序,也在使用lodash 我从api获取JSON数据。现在,让我们假设它有两个键: { foo1 : 'something', bar1 : [{.. key:value data ..}, { .. key:value data ..}, ...] } 如何为此定义接口?首先,我有一个对象数组的事实?我试过这样的方法: interface MyThing { foo : string; bar : ob

我不熟悉打字脚本,遇到了一个问题。我正在构建一个React web应用程序,也在使用lodash

我从api获取JSON数据。现在,让我们假设它有两个键:

{ 
  foo1 : 'something',
  bar1 : [{.. key:value data ..}, { .. key:value data ..}, ...]
}
如何为此定义接口?首先,我有一个对象数组的事实?我试过这样的方法:

interface MyThing {
  foo : string;
  bar : object[];
}
error TS2322: Type 'object' is not assignable to type 'MyThing[]'.
  Property 'find' is missing in type '{}'.
然后,在reducer代码中,我有如下内容:

let things : MyThing[] = _.forEach(apiData.things, (thingData) => ({
        foo : thingData.stringItem,
        bar : thingData.arrayOfObjects
    }))
我最终得到了如下错误:

interface MyThing {
  foo : string;
  bar : object[];
}
error TS2322: Type 'object' is not assignable to type 'MyThing[]'.
  Property 'find' is missing in type '{}'.
编辑: 在建议之后,我犯了一个错误,打算使用u.map,而不是u.forEach。这就是我得到的错误

ERROR in ./source/reducers/gamesReducer.ts
(37,40): error TS2345: Argument of type 'object' is not assignable to parameter of type 'List<{}> | Dictionary<{}> | NumericDictionary<{}>'.
  Type 'object' is not assignable to type 'NumericDictionary<{}>'.
    Index signature is missing in type '{}'.
./source/reducers/gamesReducer.ts中的
错误
(37,40):错误TS2345:“object”类型的参数不能分配给“List | Dictionary | NumericDictionary”类型的参数。
类型“object”不可分配给类型“NumericDictionary”。
类型“{}”中缺少索引签名。

不要使用
bar:object[]
,而是尝试
bar:{[key:string]:any}[]
,甚至只使用
bar:any[]
。另外,我认为您打算使用
.map
而不是
.forEach
.oops。对打算使用uu.map.try大写字母O ObjectI应该添加的是,用于bar1的api数据。。。每个对象项都有多个键:值对。我将代码更改为使用u.map,这是我应该做的。。现在我得到了一个不同的错误。我会更新代码。但它是这样的:
ERROR in./source/reducers/gamesReducer.ts(37,40):ERROR TS2345:object类型的参数不能分配给List | Dictionary | numericdicdictionary类型的参数。类型“object”不可分配给类型“NumericDictionary”。类型“{}”中缺少索引签名。
您能举一个例子说明
apiData
的值可能是什么样子吗?编译错误发生在哪一行?