ts-immutable-struct
ts-immutable-struct
is a TypeScript package for using immutable.js
with type-safe cursors
and simple event tracking to allow distinguishing between user-initiated state changes and
internal state changes (e.g. to sync state).
The emphasis here is on type-safety.
This package is inspired by immstruct, but uses references for everything.
React users will want to combine this with ts-react-struct, so
shouldComponentUpdate()
works with ts-immutable-struct
cursors.
Getting started
Install the package:
npm install --save ts-immutable-struct
Initialize the global state struct:
data.observe
Note that Struct()
will internally convert the given argument to an immutable.js object using immutable.fromJS()
.
Here's how to traverse and mutate the Struct
state:
// retrieve a value // update a valuedata.get'g'.get0.get'a'.val99 // alternativedata.get'g'.get0.get'a'.updatex + 1 // update the underlying immutable.js valuedata.get'g'.get0.updatex.set'a', 99
Everything here is type-checked. A wrong property name or type results in a compile-time error.