js类的getter返回值和定义

时间:2019-12-24 01:50:02

标签: javascript angular typescript ngrx

我不太了解js类的getter属性,是否必须返回js类的属性,或者我可以返回其他内容?

例如:

我有以下内容:

  constructor(
    public id: string,
    public email: string,
    public firstName?: string,
    public lastName?: string,
  ){}

  public get changeable() : string[] {
    return ['firstName', 'lastname'];
  }

}

我只想返回允许更改的字段。

但是当我在使用NgRx的同时在有角度的应用程序中执行此操作时,在reducer函数中的以下代码上出现错误:

case EmployeeActions.UPDATE_EMPLOYEE:
      const index = state.employees.findIndex(emp => emp['_id'] === action.payload._id);
      const updatedEmployees = [...state.employees];
      const updatedEmployee = 
        {
          ...state.employees[index],
          ...action.payload
        };
      updatedEmployees[index] = updatedEmployee;
      return {
        ...state, 
        employees: [...updatedEmployees],
        messages: null
      };

错误:

ERROR in src/app/employees/store/employee.reducer.ts(48,7): error TS2741: 
Property 'changeable' is missing in type 
'{ _id: string; email: string; firstName?: string; lastName?: string; }' 
but required in type 'Employee'.

0 个答案:

没有答案