Skip to content
Simple Typed Immutable Objects for Typescript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Simple Typed Immutable Objects (stimo)

Using Typescript with immutable.js Records is not really satisfying. stimo is an approach to replace Records by typescript classes using fast copy on mutate.

The design goals were:

  • stay close to hand-coded immutable objects (type compatibility)
  • minimal changes in code reading your stimo objects compared to mutable objects
  • get best typescript experience with compile errors on all mis-usages
  • do not use a generator
  • make object copy cheaper than Object.assign
  • minimize boiler plate code

stimo is inspired by



  • getter and setter must be specified with full signature
  • getter and setter must have initial dummy implementation, which will be replaced by auto-generated one
  • needs Typescript setting "experimentalDecorators": true


  • Typescript >= 1.8 (but I think it should work with other type systems as well, since decorators are ES 6 feature)
  • Need for Immutability
  • node and npm


  • npm install stimo
  • in tsconfig.json set CompilerOption "experimentalDecorators": true


class MyRecord {
  @stimo_get get title(): string { return null; /* dummy for compiler */};
  @stimo_set setTitle(title:string): MyRecord { return null;/* dummy for compiler */};
  @stimo_get get id(): number { return null; /* dummy for compiler */};
  @stimo_set setId(id:number): MyRecord { return null;/* dummy for compiler */};

  constructor(title: string, id:number) {
  // write your own methods
  dump():string {
    return this.title + '(' + + ')';

You can then use your immutable stimo Object like this:

const m:MyRecord = new MyRecord("Test",5);
// standard typed property access
m.title = 5; // compile error
const c:number = m.title; // compile error
m.setTitle(5); // compile error

// setter returns new mutated object
const n:MyRecord = m.setTitle("newTitle");
expect(m !== n);
  • Make sure to use @stimo on the class, otherwise it will not work
  • You will have full code-completion and compile time type checking support, no operations with magic strings or accidental call of setters
  • constructor will call setters inplace for performance
  • Outside constructor setters will return a new instance if the value was mutated


After checking lots of options for typing immutable objects, I was left disappointed.

Thinking about using decorators to generated the access code, I found, which seemed close to what we need for our application. Porting the application to doop was not real fun, because of the accessor syntax of doop, so I decided to write something myself.

stimo has more boilerplate than doop, but has some advantages: stronger typing, property assignment will be flagged as error by compiler, type compatibility with handcoded immutable object.

If you have any ideas in further reducing boilerplate, please contribute.

References of other approaches trying to solve the same problem (none of them I found satisfying):

Implementations and Tests for different approaches creating typed immutable object can be found in subdirectory 'testimmutability' in this repo.

All are commented with pros/cons of the respective approach.

You can’t perform that action at this time.