mobxjs / mobx-state-tree

Full-featured reactive state management without the boilerplate
https://mobx-state-tree.js.org/
MIT License
6.94k stars 641 forks source link

Instance node can't be used to instantiate a reference #1745

Open adamkovalsky opened 3 years ago

adamkovalsky commented 3 years ago

Bug report

Sandbox link or minimal reproduction code

const ModelA = types.model({
  id: types.identifier,
  n: types.number
}).actions(self => ({
  setN(aNumber: number) {
    self.n = aNumber
  }
}))

const ModelB = types.model({
  refA: types.reference(ModelA)
})

const a = ModelA.create({ id: 'someId', n: 5 });
// this will allow the compiler to use a model as if it were a reference snapshot
const b = ModelB.create({ refA: castToReferenceSnapshot(a)})

console.log(b.refA.n);

Describe the expected behavior 5 should be logged to the console.

Describe the observed behavior [mobx-state-tree] Failed to resolve reference 'someId' to type 'AnonymousModel' (from node: /refA)

Notes This example code comes directly from the documentation of castToReferenceSnapshot: https://mobx-state-tree.js.org/API/#casttoreferencesnapshot

jamonholmgren commented 3 years ago

Good call. Would like to get this fixed.