【发布时间】:2019-03-28 17:00:46
【问题描述】:
我的 Angular 应用中有 employees 对象数组。我需要为它输入 type 而不是 any,所以我定义了 interface。但我得到如下错误。
src/app/newComp/employee/employee.component.ts(11,3) 中的错误:错误 TS2322:类型 '{ empID: string;名称:字符串;性别:字符串;工资:数字;出生日期:字符串; }[]' 不可分配给类型“员工”。 '{ empID: string; 类型中缺少属性 'empID'名称:字符串;性别:字符串;工资:数字;出生日期:字符串; }[]'。 src/app/newComp/employee/employee.component.ts(20,7): 错误 TS2322: Type '{ empID: string;名称:字符串;性别:字符串;工资:数字;出生日期:字符串; }[]' 不可分配给类型“员工”。 '{ empID: string; 类型中缺少属性 'empID'名称:字符串;性别:字符串;工资:数字;出生日期:字符串; }[]'。
如何解决这个问题?这是我的全部代码。
export class EmployeeComponent {
employees: Employees;
constructor() {
this.employees = [
{ empID: 'empid101', name: 'Jhon', gender: 'male', salary: 1200, DOB: '12/24/2016' },
{ empID: 'empid102', name: 'Nancy', gender: 'female', salary: 2445.23, DOB: '4/2/2016' },
{ empID: 'empid103', name: 'Julie', gender: 'female', salary: 5000.23, DOB: '4/14/2016' },
{ empID: 'empid104', name: 'Brito', gender: 'male', salary: 4352, DOB: '5/12/2016' }
];
}
/* tslint:disable */
refreshTheData(): void {
this.employees = [
{ empID: 'empid101', name: 'Jhon', gender: 'male', salary: 1200, DOB: '12/24/2016' },
{ empID: 'empid102', name: 'Nancy', gender: 'female', salary: 2445.23, DOB: '4/2/2016' },
{ empID: 'empid103', name: 'Julie', gender: 'female', salary: 5000.23, DOB: '4/14/2016' },
{ empID: 'empid104', name: 'Brito', gender: 'male', salary: 4352, DOB: '5/12/2016' },
{ empID: 'empid105', name: 'Clark', gender: 'male', salary: 7543, DOB: '2/15/1990' } ]; }
interface Employees{
empID: string,
name: string,
gender: string,
salary: number,
DOB: Date
}
interface Employees extends Array<Employees>{}
【问题讨论】:
标签: angular typescript interface