声明数组时出现打字稿错误:类型'Report []'缺少类型…

时间:2019-07-17 04:49:57

标签: javascript reactjs typescript ecmascript-6

我有这个虚拟数据,我想输入:

export default interface Report {
  id: number;
  name: string;
  lastName: string;
}

export const dummyReports: Report[] = [
  { id: 23, name: 'Carlo', lastName: 'Magno' },
  { id: 11, name: 'Albert', lastName: 'Camus' },
  { id: 51, name: 'Oliver', lastName: 'Khan' }
];

export const dummyReportsArr: Report[] = [
  dummyReports, // THIS IS THE LINE THROWING THE ERROR
  { ...dummyReports, id: 9000 }
];

Type 'Report[]' is missing the following properties from type 'Report': id, name, lastName ts(2739)

我想念什么?

1 个答案:

答案 0 :(得分:0)

您已将dummyReportsArr设置为Report[]类型。但是,您要为其分配2D数组

dummyReportsArr = [
  [ {}, {}, {} ], // dummyReports
  { 0: {}, 1: {}, 2: {}, id: 9000 } // an object with numeric indices
]

数组中的第一项是dummyReports数组。第二个是对象。在{}内扩展数组会创建一个对象,该对象的数组索引为对象的键。这些都不是Report

类型