【问题标题】:NGRX state spread operator Type must have a '[Symbol.iterator]()'NGRX 状态扩展运算符类型必须有一个 '[Symbol.iterator]()'
【发布时间】:2018-06-28 16:12:20
【问题描述】:

我正在使用 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
}

【问题讨论】:

    标签: angular typescript ngrx


    【解决方案1】:

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

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

    这相当于使用这样的对象传播:

    return { ...state, eventsLoading: true }
    

    Object.assign 合并多个对象,而object spread 将对象的键扩展为对象字面量。在大多数情况下,对象传播使Object.assign 过时了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-16
      • 1970-01-01
      • 2019-04-28
      • 1970-01-01
      • 1970-01-01
      • 2021-04-07
      • 2010-11-15
      • 2020-03-19
      相关资源
      最近更新 更多