We have a problem Immutable.jsin conjunction with TypeScript, and your opinion is evaluated:
Last year, we are working on a response-redux application.
As recommended in almost every best practice, common sense and redux docs , our storage data is immutable.
As recommended in the above redux documents used in many large projects, and when using many templates, we use Immutable.js.
We are also writing our application in TypeScript.
The problem is that these two ( Immutable.jsand TypeScript) can not play well. After using toJSor getor getInto convert an immutable object to a regular JS object, the type changes to any, and this violates the type safety between the repository and the component.
We tried
state.users.getIn(['counts', visits]) as numberbut we were hoping for better type safety.
- Correct
Immutable.jsinput:
We also tried to introduce it ourselves, and sometimes we got to
interface ImmutableMap<T> {
get<K extends keyof T>(name: K): T[K];
set<S>(k: string, o: S): ImmutableMap<T & S>;
getIn(searchKeyPath: any[], notSetValue?: any): T;
}
(idea here )
Assuming that it getInis only called with toJSand always returns a JS object (which we sometimes do in our higher-order-component - but that's a different story).
.
, Immutable.js. *
- Vanilla:
, :
{
...state,
profiles: {
...state.profiles,
[id]: {
...state.profiles[id],
meta: {
...state.profiles[id].meta,
[sub]: payload,
},
},
},
Immutable.js, - .
record - js- , TypeScript , , , API, JS. , .
, / React-Redux:
- - ?
Immutable.js TypeScript? - - /, ?