angular - 错误 : "Property ' setIn' does not exist on type 'Map<any, any>' "

标签 angular redux immutable.js

我是 Immutable.js 和 Redux(以及 Angular2)的(相当)新手,在标题中收到错误(setIn 和 getIn 都收到错误)。错误来 self 的 store.ts。这是简短的版本:

import { Action as reduxAction, Reducer, combineReducers } from 'redux';
import { ActionActions } from './actions/action.actions';
import { UserActions } from './actions/user.actions';
import { MetaActions } from './actions/meta.actions';
import { Action } from './app/library/action/class.action';
import * as Immutable from 'immutable';
import 'rxjs/add/operator/map';

export interface IAppState {
    action: IActionState
}

export interface IActionState {
    action?: Map<any, any>
}

export const INITIAL_STATE: IAppState = {
    action: {
        action: null
    }
}

export function actionReducer(lastState: IActionState, action) 
{
    // Initial action state
    if (undefined === lastState) {
        lastState = {
            action: null
        }
    }

    // Switching
    switch(action.type)
    {
        case ActionActions.REMOVE_EMPLOYEE:
        {
            return { action: lastState.action.setIn(['employees', action.value.role], 
                lastState.action.getIn(['employees', action.value.role])
                .filter(o => o.getIn(['employee','employeeId']) !== action.value.employeeId)) };
        }  
    }
    
    return { action: lastState.action }
}

(请不要混淆我正在处理的对象也恰好被命名为“action”。这在 Redux 的上下文中有点不吉利,但在应用程序的上下文中确实有意义。 )

当编译器(转译器?)在终端中运行时,我在标题中收到错误,但当我运行网站时脚本运行正常。完全没有问题。只是终端中的错误。

如果我尝试在操作对象上运行 Immutable.map.isMap(),它会返回 true。方法 get() 和 set() 似乎也存在于对象上。

所以如果它实际上是一个 Map 并且 set() 和 get() 有效——为什么 setIn() 和 getIn() 不行呢?需要明确的是 - 它们确实有效 - 非常完美。我只是在终端中收到一个错误。

我可以在应用程序的其他地方使用 setIn() 和 getIn()。即使在同一个物体上。那么为什么不在 store.ts 中呢?我的进口有问题吗?

最佳答案

好的 - 在前额和墙壁之间进行了多次密集 session 之后,我似乎已经自己解决了这个问题。

我这样导入了 Immutable.js 库:

import * as Immutable from 'immutable';

然后我像这样实例化了对象的(接口(interface)):

action?: Map<any, any>

应该是什么时候

action?: Immutable.Map<any, any>

显然这可行。

这对某些人来说似乎很明显,但我被抛弃了,因为 Immutable.map.isMap() 评估为真。

关于angular - 错误 : "Property ' setIn' does not exist on type 'Map<any, any>' ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46987377/

相关文章:

indexing - Angular2 *ngFor 将索引设置为同一标签内的属性

angular - 错误 'mat-dialog-content' 不是已知元素

angular - Redux - 如何在大型应用程序中组织商店

angular - 如何处理带有离线数据的ngrx/store?

javascript - Immutable.js 记录上的方法会复制吗?

node.js - 我无法将 Angular/Cli 安装到我的 ubuntu 18 系统上

javascript - 使用外部 javascript lib footable 时丢失 Angular 2 中的点击事件

reactjs - React-Redux |产品列表 |使用 BindActionCreator 添加/删除

javascript - 用于提升 React 性能的 Immutable.js 替代方案

javascript - Redux 和 immutable js,我在尝试使用 immutable 后失去了状态