Table of Contents
The module is on NPM and has a dependency of Immutable.js package version 3.x.x. Thus you need to install both in order to use this library.
npm install --save immutablenpm install --save typed-immutable-record
This module exposes a called
TypedRecord<T> interface. This interface is an implementation of Immutable.Map that adjusts the return type of each function to the provided
<T> type. In other words, all mutating operations in the Immutable.Record that would return a new version of itself is going to return a new version of
The following example shows how it looks in practice. The first step is import the interface from
typed-immutable-record package and extend it.
This is half of everything you need to do. The only important thing to note is that there is still no benefit since the shape of the object you want to make Immutable is not yet defined. Thus, for the sake of simplicity, suppose our model can be described with two attributes:
Animal) and a
TypedRecord<T>, but they are isolated and have no relationship with each other. Basically, we want the shape to be part of the Record so that its properties can be accessed. This can be easily done with a simple change in the
The difference is that now the
IAnimalRecord is also an
IAnimal and an Immutable.Record.
Based on both animals interfaces created above, we could generate a record in the following way:
Remember that Immutable.Records have a default value for every property and deleting that property sets its value to the default value, and not
undefined. In the example above, deleting the property
age from the
catRecord will make its value
Sometimes we don't care about the default state of an Immutable.Record, we just want that a plain object becomes a Record with the values it currently has. To support that, another function is exposed from
typed-immutable-record that can generate a Immutable.Record in a one step operation.
Let us take a look how we can do that using the same