npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

immuts

v2.1.1

Published

Simple, type-safe immutable data structure for Typescript

Downloads

87

Readme

immuts

Type-safe, generic immutable datastructure for Typescript. Does not require manually setting JS paths ["a", "b", "c"] and allows TS autocompleted drilldown.

Changelog

  • 2.1.0 - Adding some array helpers
  • 2.0.1 - Vastly simplified
  • 0.4.5 - Added some methods for dealing with native arrays
  • 0.4.0 - Removed incomplete immutablejs adapter for now
  • 0.3.0 - Improved support for working with arrays and maps

Usage

Simple

interface IA {
    id: number;
    name: string;
}

let a1 = makeImmutable<IA>({  
    id: 42,
    name: "foo"
});

let a2 = a1.__set(x => x.id, 23);

// a1 !== a2 => true

let a3 = a2.__set(x => x.id, "23"); // Results in compiler error, string cannot be assigned to number

Updating multiple properties

interface IA {
    id: number;
    name: string;
}

interface IB {
    a1: IA;
    a2: IA;
}

interface IC {
    b: IB;
}

let c = makeImmutable<IC>({
        b: {
            a1: {  
                id: 42,
                name: "foo"
            }, 
            a2: {
                id: 23,
                name: "bar"
            }
        }
    });

let c2 = c.__set(x => x.b.a1, x => ({
    ...x,
    id: 23
}));

// c2.b.a1.id === 23 => true
// c2.b.a1.name === "foo" => true

Nested

The integration is most valuable when used with a nested object:

interface IA {
    id: number;
    name: string;
}

interface IB {
    a1: IA;
    a2: IA;
}

interface IC {
    b: IB;
}

let c = makeImmutable<IC>({
        b: {
            a1: {  
                id: 42,
                name: "foo"
            }, 
            a2: {
                id: 23,
                name: "bar"
            }
        }
    });

let c2 = c.__set(x => x.b.a1.id, 12);

// c !== c2 => true

// c.b.a2 === c2.b.a2 => true
// c.b.a1 !== c2.b.a1 => true

when you execute this:

let c2 = c.__set(x => x.b.a1.id, 12);

the root, b, and a1 will be automatically cloned, before the new id is assigned to a1. And again, everything is type-safe, something like

let c4 = c.__set(x => x.b.a1.id, "12");

would result in a compiler error, because the types of id and "12" do not match.

Maps

Add element

const c1 = makeImmutable({
    foo: {
        "a": 42,
        "b": 23
    }
});

const c2 = c.__set(x => x.foo, x => ({
    ...x,
    "c": 11
}));

// c1.foo !== c2.foo => true
// c2.foo deep equals { "a": 42, "b": 23, "c": 11 }

Remove element

const c1 = makeImmutable({
    foo: {
        "a": 42,
        "b": 23
    }
});

// Remove b
const p = "b";
const c2 = c.__set(x => x.foo, ({ [p], ...r }) => r);

// c1.foo !== c2.foo => true
// c2.foo deep equals { "a": 42 }

Arrays

immuts includes a few helpers for common array operations, returning new versions of the modified arrays: push, pop, splice, remove.

const c1 = makeImmutable({
    foo: [1, 2]
});

const c2 = c1.__set(x => x.foo, x => push(x, 3));

// c2.foo !== c1.foo
// c2.foo deep equals [1,2,3]

const c3 = c2.__set(x => x.foo, x => remove(x, 1));

// c3.foo !== c2.foo
// c3.foo deep equals [1,3]

Limitations

Internet Explorer and undefined

To build up the property path (i.__set(x => x.a.b.c) needs to be captured into ["a", "b", "c"]) the library relies on the ES6 Proxy object. In browsers where this is not suppored (mainly all versions of Internet Explorer) a fallback is used using Object.defineProperty.

This method does not deal correctly with optional properties, so something like this:

interface IA {    
    foo?: string;
    bar: number;
}

let i = new Immutable<IA>({
    // foo: "test", - leave undefined! 
    bar: 42
});

i.__set(x => x.foo, "test2");

would fail because foo did not exist at the time of creation. If you don't target Internet Explorer this will not be an issue and everything should work just fine, otherwise do not use optional properties, initialize to null.