NGRX状态扩展运算符类型必须具有“ [Symbol.iterator]()”

时间:2018-06-28 16:12:21

标签: angular typescript ngrx

我正在使用NGRX实体适配器初始化状态(问题仅在getInitialState处发生)。

 export const initialState = adapter.getInitialState({
      eventsError: null,
      eventsLoading: false
    });

    export function reducer(
      state = initialState,
      action: EventsActions
    ): State {
      switch (action.type) {

        case EventsActionTypes.getAllEvents: {
          return Object.assign({}, ...state, { // error line
            eventsLoading: true
          });
        }
// ...

当我想在状态对象上使用传播运算符时,出现错误:

ERROR in src/app/events/reducers/events.reducer.ts(36,35): error TS2488: Type must have a '[Symbol.iterator]()' method that returns an iterator.

这是我的tsconfig.json文件

    {
  "compilerOptions": {
    "noImplicitAny": true,
    "removeComments": true,
    "sourceMap": true,
    "target": "es6",
    "module": "commonjs",
    "experimentalDecorators": true,
    "noEmitHelpers": false,
    "emitDecoratorMetadata": true,
    "declaration": false,
    "lib": [
      "es2015",
      "dom"
    ],
    "moduleResolution": "node",
    "suppressImplicitAnyIndexErrors": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true
  },
  "compileOnSave": false,
  "buildOnSave": false
}

1 个答案:

答案 0 :(得分:3)

如果您使用Object.assign,则不要使用点差运算符:

return Object.assign({}, state, { eventsLoading: true });

相当于使用对象散布如下:

return { ...state, eventsLoading: true }

Object.assign合并多个对象,其中object spread将对象的键扩展为对象常量。大多数情况下,对象传播使Object.assign变得过时。