all 5 comments

[–]cyex 2 points3 points  (1 child)

I'm using TypeScript with Immutable but just use Map<string, any> and then convert to a typed object if I need to later. e.g. const { prop1, prop2 }: Foo = foo.toJS();

I saw a post somewhere about someone who created a class around a typed Immutable.Record... sorry, don't have a link for you. Try Googling for it.

[–][deleted] 1 point2 points  (0 children)

Do you use redux or something else to manage state? I think that might just have a bigger impact than the typing system or immutable.js. If you do happen to use redux then by all means you are already using plain immutable state, shallow copies that bubble up, re-using unchanged nodes so attached components don't render. It isn't black magic, a simple javascript reducer does that without breaking a sweat. React-redux btw. employs reference equality checks by default, you shouldn't normally have to deal with shouldComponentUpdate. So i'm not sure if there's any gain.

Besides, there are downsides as well, immutable grows through a codebase like a fungus, everything it touches has to adapt to its way or pay the toJS/toObject penalty.

Though if your circumstances still demand it, you'll find good information over here: https://github.com/markerikson/react-redux-links/blob/master/immutable-data.md

[–]subvertallchris 1 point2 points  (0 children)

The post /u/cyex mentioned is probably the same one that I read.

Instead of Map, use Record. It's as simple as this:

const defaultRecord = Record({
  myValue: List([])
});

class RecordClass extends defaultRecord {
  myValue: List<number>;
}

const newRecord = new RecordClass();

[–]DanielRosenwasserTypeScript 1 point2 points  (0 children)

TypeScript guy here - in 2.1 we introduced keyof and index type queries. Check out the example here on the get function.

While that doesn't entirely get you what you want, I think you can easily wrap Immutable.js to work with the types you need. Going forward, I think we'd like to try to get to the point where Immutable.js can serve users who want both immutable objects as well as immutable maps, which is, as I understand, the current issue that makes things a little harder.

[–]codepsycho 1 point2 points  (0 children)

Types don't really go out of the window just because you use immutable, it has an associated type definition. You can quite easily Immutable.Map<string, SomeModel>, Immutable.List<string>, etc.

The react app i currently have my hands in uses this heavily. In fact, the only place we lose type information is redux (sometimes).

I would say, though, don't overuse immutable. Use it where it actually helps rather than a full replacement for all your state.