mobx-keystone icon indicating copy to clipboard operation
mobx-keystone copied to clipboard

No static checks to prevent assignment outside of an action

Open advdv opened this issue 1 year ago • 1 comments

Hey! You've setup a great library, thank you for that. I was wondering if I'm doing something wrong or if I'm not understanding something. When I create a very basic store:

import { prop, model, Model, modelAction } from "mobx-keystone";

@model("AtsDash/Debug")
export class Debug extends Model({
  count: prop<number>(),
}) {
  @modelAction
  increment() {
    this.count += 1;
  }

  @modelAction
  decrement() {
    this.count -= 1;
  }
}

And then try to use it like this:

import { describe, expect, it } from "vitest";
import { Debug } from "./model";

describe("model", () => {
  it("should work", () => {
    expect.assertions(1);

    const store = new Debug({ count: 0 });

    store.count = 1; // this will compile, no problem

    expect(store.count).toBe(1);
  });
});

It will not complain about me setting a property outside of an action when compiling. It will only complain about this at runtime. Maybe this is working as intended but since good typescript support is a feature of this library I thought I would ask.

Thank you

advdv avatar Apr 27 '24 05:04 advdv

Basically because for typescript there's no difference between the count in this and the count in store ☹️

xaviergonz avatar Apr 27 '24 20:04 xaviergonz

Too bad, but thank you for the quick response.

advdv avatar Apr 29 '24 18:04 advdv