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

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

Open Amareis opened this issue 4 years ago • 10 comments

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

Amareis avatar Aug 11 '20 15:08 Amareis

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

xaviergonz avatar Aug 11 '20 22:08 xaviergonz

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

xaviergonz avatar Aug 11 '20 22:08 xaviergonz

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.

Amareis avatar Aug 12 '20 10:08 Amareis

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

xaviergonz avatar Aug 16 '20 22:08 xaviergonz

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

Amareis avatar Aug 25 '20 23:08 Amareis

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

Amareis avatar Aug 25 '20 23:08 Amareis

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

xaviergonz avatar Aug 26 '20 09:08 xaviergonz

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

vojto avatar Jul 08 '22 09:07 vojto