mobxjs / mobx-state-tree

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

Improve typing for model type substitutability. #2189

Closed thegedge closed 2 months ago

thegedge commented 3 months ago

Previously, a model derived from an empty model couldn't be used in a place that asked for the empty model. In general, we had a lot of substitutability issues (just with our typing).

This commit fixes that by bringing back what was previously known as "$nonEmptyObject", but improves on it by not having it be as pervasive as it was previously (for example, it would show up on models that had props).

What does this PR do and why?

Fixes #2186

This PR fixes regressions introduced (by me! 😅) in the previous major release that attempted to remove $nonEmptyObject from all types. Little did we know, it did support stricter typing in some cases.

This PR reintroduces a similar concept, but it does so in a far more limited form to fix the aforementioned issue.

Steps to validate locally

The test is a good example, and validating would simply be running the type checker.

coolsoftwaretyler commented 3 months ago

Thanks, @thegedge! I'll take a look this weekend and merge/release a patch shortly.