Model instance maybe fields becoming TypeScript optional fields when included in a types.union #1525
Closed
3 tasks done
Labels
help/PR welcome
Help/Pull request from contributors to fix the issue is welcome
Typescript
Issue related to Typescript typings
Bug report
Sandbox link or minimal reproduction code
https://codesandbox.io/s/wispy-meadow-z49x9
I've pinned the MST and Mobx versions in the reproduction, so that the issue can be reproduced if new versions affect the behaviour in any way (good or bad!).
Describe the expected behavior
That the TypeScript interface derived from
Instance<typeof Model>
would always "fit" an instance of that model.Describe the observed behavior
When a child model has an a "maybe" field, and is then used within a union type, attempting to pass an instance of this model from the union field to a React prop typed as
Instance<typeof Model>
fails with "Property '...' is optional in type ... but required in type 'IModel'
"It's probably easiest to view the linked reproduction code.
I think what's happening is that when the model type is coming from within the union, "maybe" fields are becoming optional fields - but they are still mandatory fields on the derived interface. I can see that typescript reports the model type differently when it's come from the union vs. when it has not.
I realise this is a pretty specific use case, but I thought it worth raising. If there's any issue with the reproduction or more information is required, please let me know!
The text was updated successfully, but these errors were encountered: