我正在使用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
}
答案 0 :(得分:3)
如果您使用Object.assign
,则不要使用点差运算符:
return Object.assign({}, state, { eventsLoading: true });
相当于使用对象散布如下:
return { ...state, eventsLoading: true }
Object.assign
合并多个对象,其中object spread将对象的键扩展为对象常量。大多数情况下,对象传播使Object.assign
变得过时。