typed-immutable-record
typed-immutable-record
lets you combine the advantages of Typescript interfaces with Immutable Records, offering an easy and clean way to build immutable objects from plain Javascript objects ensuring types assertion.
Table of Contents
Installation
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
Quick Start
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 <T>
instead.
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:
Lastly we have the shape of an acceptable javascript object (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 AnimalRecord
interface:
The difference is that now the IAnimalRecord
is also an IAnimal
and an Immutable.Record.
Usage
typed-immutable-record
also exposes a factory function, used to make Immutable.Records from a plain Javascript object. The usage is pretty straightforward and requires the user to provide the generic information to the Typescript compiler.
Based on both animals interfaces created above, we could generate a record in the following way:
; /* create a plain javascript object that meets the requirements of the IAnimal interface and represents the default values of the Immutable.Record */; /* make the factory to enable the generation of animal records */; /* create a plain javascript animal object */; /* create the typed record! */; /* performing updates on the record returns another IAnimalRecord and it will still be assignable to functions that requires IAnimal or IAnimalRecord */;console.logdogRecord.type; // 'Dog'console.logdogRecord.age; // 9 /* It will also keep the original record factory properties, so that whenever you remove a property it defaults to the value when the factory was created */;console.logpuppyRecord.age; // 0console.logpuppyRecord.type; // 'Dog'
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 0
.
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 IAnimalRecord
:
; ;