xaviergonz / mobx-keystone

A MobX powered state management solution based on data trees with first class support for Typescript, support for snapshots, patches and much more
https://mobx-keystone.js.org
MIT License
537 stars 23 forks source link

Model, created in itself static method, doesn't work properly when using legacy decorators transpiled by babel #173

Open Amareis opened 3 years ago

Amareis commented 3 years ago

https://codesandbox.io/s/mobx-keystone-static-method-problem-4thb2?file=/src/index.tsx

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

@model("BuggyModel")
class BuggyModel extends Model({
  value: tProp("1")
}) {
  static foo() {
    return new BuggyModel({});
  }
}

const a = BuggyModel.foo();
a.value = null as any; //don't throws!
console.log(a); //there is no modelInitializedSymbol in instance

const b = new BuggyModel({})
b.value = null as any; //throws, ok

I think it's coupled with babel, because I cannot reproduce it in pure ts tests. Possible related #18 #92

xaviergonz commented 3 years ago

I think it is this babel decorator plugin bug: https://github.com/babel/babel/issues/11131

xaviergonz commented 3 years ago

Also related: https://github.com/xaviergonz/mobx-keystone/issues/122

Amareis commented 3 years ago

Yep, definitely it's babel bug. Currently i use external method and assign it into static property

static foo = foo

Can we fix this babel bug? I'll try to look into this.

xaviergonz commented 3 years ago

If you were to fix the babel plugin that'd be awesome :) Not much that can be done on the mobx-keystone repo sadly

Amareis commented 3 years ago

Uh-oh, so I fixed plugin, but not pushed it yet.

Amareis commented 3 years ago

https://github.com/babel/babel/pull/12007

xaviergonz commented 3 years ago

That's awesome! Let's hope it gets merged soon 👍

vojto commented 2 years ago

Workarounds in that issue aren't very appealing. I think we'll just avoid static methods for now.

samskiter commented 3 weeks ago

womp womp - is this still an issue?

xaviergonz commented 3 weeks ago

It never got fixed by babel, so yes. I don't know if it happens with the new (non legacy) decorators though.