A new type-safe structure for shallow compare in javascript

Zack
1 min readJul 11, 2017

--

I have been suffering from operating with immutable data in React/Redux stack for a long while, I tried many immutable libraries, even made one for myself(https://github.com/zaaack/immuter), it looks like others, maybe simpler in some part (or not). But the big pain isn’t solved, if you writing getters/setters with key path, then your type checker lose type. Your App’s state becomes a dark, dangerous place in the whole type covered land.

Then I find an interesting library https://github.com/kube/monolite, it makes immutable setting nested object works with typescript. It’s using es6 Proxy under the hood. This inspired me, why we cannot create a more clean syntax (and support both ts/flow)? Then Immuter got a new feature:

Type-Safe Struct.

It looks like deep clone, but works like immutable-js.

More code:

The interface is simple, easy to understand, and more important, it’s type checker friendly.

You can see how more information in README: https://github.com/zaaack/immuter#new-type-safe-struct-feature

And since the structure is fixed, we can using polyfill to support even IE9! (Using Map for uncertain key).

It’s just like a language level support of immutable date type, optimized, type-safe, but for javascript.

--

--