Omnistac / zedux

:zap: A Molecular State Engine for React
https://Omnistac.github.io/zedux/
MIT License
344 stars 7 forks source link

fix(core): allow store-extending classes to have array state types #96

Closed bowheart closed 3 months ago

bowheart commented 4 months ago

Description

When extending the core Store class and instantiating your class with an array type, TS gives a super cryptic error message that basically boils down to: store.use() doesn't accept a hierarchy descriptor type where the State generic is an array.

Solution

Make store.use() accept a different type that infers the hierarchy descriptor type better since it's know at that point. Prevent the new type's union from containing Branch if the state type at a given level is an array. Even though store.use() technically allows swapping an array out for more nested objects, there should never be a real use case for that and TS land really doesn't need to know about it.

The Past and the Future

store.use() wasn't designed with TS in mind. It used to be the primary mechanism for code-splitting in Zedux before atoms. Now it's pretty useless with atoms and is only used to swap out destroyed child stores with their new instances. We should maybe consider a better API for that use case in Zedux v2.