Open swayf opened 5 years ago
That's because upon ".create", the refinement (validation) is run first, then it will instantiate that node as type "TestType", so getType will return that as type for that node. To fix this the refinement would need to be kept as type for the node (doable, but not easy).
Not sure if a bug or an enhancement :)
We've found that you can use types.compose
to change the name of the refined model, which doesn't help with dynamically creating new instances but does allow us to reflect on the type of a model created from a snapshot:
const TestType2 = types.refinement(
types.compose("TestType2", TestType),
value => value.test1 + value.test2 === 100
);
const testModel2 = TestType2.create({ test1: 50, test2: 50 });
getType(testModel2).name; // 'TestType2'
Using types.compose
with just one model feels hacky, but that code does seem to work.
Bug report
Sandbox link or minimal reproduction code
https://codesandbox.io/s/mobx-state-tree-todolist-52jt7
Describe the expected behavior
last expression should fail (or?)
Describe the observed behavior
but it does not, because getType returns type before refinement