mobx-keystone
mobx-keystone copied to clipboard
Model, created in itself static method, doesn't work properly when using legacy decorators transpiled by babel
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
I think it is this babel decorator plugin bug: https://github.com/babel/babel/issues/11131
Also related: https://github.com/xaviergonz/mobx-keystone/issues/122
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.
If you were to fix the babel plugin that'd be awesome :) Not much that can be done on the mobx-keystone repo sadly
Uh-oh, so I fixed plugin, but not pushed it yet.
https://github.com/babel/babel/pull/12007
That's awesome! Let's hope it gets merged soon 👍
Workarounds in that issue aren't very appealing. I think we'll just avoid static methods for now.