mobx-state-tree
mobx-state-tree copied to clipboard
Accessing a field of a model that inherits from another leads to a missing type error
Bug report
- [x] I've checked documentation and searched for existing issues
- [x] I've made sure my project is based on the latest MST version
- [x] Fork this code sandbox or another minimal reproduction.
Sandbox link or minimal reproduction code
The error can be seen in particular at the above code sandbox in the Person.ts file.
Describe the expected behavior
When using getRoot to access a model that is composed over a variety of others e.g. a model of type Food where
export const Food = types.union({ dispatcher }, Fruit, Orange, Apple);
and Orange and Apple are created via Fruit.props({...}), I'd expect that properties that only exist on one of the "child" models would be optional i.e. would return as undefined if the runtime model was one without that field. In the sandbox linked to for example I would expect to be able to call
const {
food: { seeded }
} = getRoot<IRoot>(self);
and for seeded to be undefined for an Apple and a boolean for an Orange.
Describe the observed behavior
Only the properties from the "Parent" model (in this case the Fruit model) are recognised as valid properties by Typescript.
For example, in the code sandbox example when trying to access the seeded property there is a compilation error that seeded does not exist on the type.
Looking at the error it seems to be using an intersection of properties rather than a union.
Ran into this the other day. Types in dispatcher really intersect each other.
Thanks for the initial write up @buck06191, and I'm sorry that it took so long for us to get back to you! (@AaronPorts - I appreciate you jumping in here as well).
This seems to be a TypeScript issue, so I'm going to label it as such. We are definitely open for PRs to improve typings, so I'll add a label to that effect as well.
If either of y'all are interested in helping out, that would be great. But since it's been so long, no worries if not! We will find time or someone to resolve this eventually.