如何在Redux和TypeScript中使用Immutable.js?

时间:2016-07-21 22:30:32

标签: typescript angular redux immutable.js

我正在尝试将我的状态对象更改为不可变的Immutable.js对象,但我在类型定义方面遇到了很大的麻烦。我试图在网上找到类似的问题,但实际上找不到任何有用的东西。

我有以下状态,我想将其转换为Immmutable对象。

export interface PostState {
  ids: number[];
  entities: { [id: number]: Post};
  loading: boolean;
  error: string;
};

export interface Post {
  id: number;
  comments: {
    ids: number[];
    loading: boolean;
  }
};

有什么建议吗?我应该如何定义这些接口以确保Immutable.js的不变性?

1 个答案:

答案 0 :(得分:0)

  

我想将其转换为Immmutable对象。

基本

两个优秀的JavaScript结构是:Object(用作Dictionary,也称为HashMap,也称为Map)和Array(也称为{{ 1}}等)。

样品

在你所在州,你有一张地图:

List

和一些阵列,例如

entities: { [id: number]: Post};

转换

对于Map,Immutable有自己的结构:https://facebook.github.io/immutable-js/docs/#/Map

对于List,有:https://facebook.github.io/immutable-js/docs/#/List

声明

正是您所期望的,例如ids: number[];

List - > ids: number[];