Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing a field of a model that inherits from another leads to a missing type error #1833

Open
3 tasks done
buck06191 opened this issue Nov 17, 2021 · 2 comments
Open
3 tasks done
Labels
help/PR welcome Help/Pull request from contributors to fix the issue is welcome Typescript Issue related to Typescript typings

Comments

@buck06191
Copy link

Bug report

  • I've checked documentation and searched for existing issues
  • I've made sure my project is based on the latest MST version
  • 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.
image
Looking at the error it seems to be using an intersection of properties rather than a union.

@AaronPorts
Copy link

Ran into this the other day. Types in dispatcher really intersect each other.

@coolsoftwaretyler
Copy link
Collaborator

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.

@coolsoftwaretyler coolsoftwaretyler added help/PR welcome Help/Pull request from contributors to fix the issue is welcome Typescript Issue related to Typescript typings labels Jun 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help/PR welcome Help/Pull request from contributors to fix the issue is welcome Typescript Issue related to Typescript typings
Projects
None yet
Development

No branches or pull requests

3 participants