Neovictorian Paisley Menswear

    ts-imm
    TypeScript icon, indicating that this package has built-in type declarations

    0.4.0 • Public • Published

    ts-imm

    Simple data classes in TypeScript, modeled after case classes in Scala.

    Usage

    class SimpleData extends Imm<SimpleData> {
      @imm public name!: string;
      @imm public address?: string;
     
      public someBusinessLogic(): this {
        return this.copy({name: "new name"});
      }
    }
     
    describe("imm", () => {
      it("can create a new instance", () => {
        const d = new SimpleData({name: "a"});
        expect(d.name).toEqual("a");
        expect(d.address).toBeUndefined();
      });
     
      it("can someBusinessLogic a property with a method", () => {
        const a = new SimpleData({name: "a"});
        expect(a.name).toEqual("a");
        const b = a.someBusinessLogic();
        expect(a.name).toEqual("a");
        expect(b.name).toEqual("new name");
      });
     
      it("can someBusinessLogic a property with the generic copy constructor", () => {
        const a = new SimpleData({name: "a"});
        expect(a.address).toBeUndefined();
        const b = a.copy({address: "b"});
        expect(a.address).toBeUndefined();
        expect(b.address).toEqual("b");
      });
     
    });

    Disclaimers

    • Uses an immutable.js map under the hood; in theory this is good, but I'm not an expert on that vs. just a Object.assign
    • I've not done any testing/thinking about inheritance; in case classes this was a bad idea
    • The intent is not to model your entire tree of Redux stores, this is "just data classes"

    Credits

    Inspiration from stimo.

    Keywords

    Install

    npm i ts-imm

    DownloadsWeekly Downloads

    4,189

    Version

    0.4.0

    License

    Apache-2.0

    Unpacked Size

    46.6 kB

    Total Files

    21

    Last publish

    Collaborators

    • shaberman