How to split store in different files? And the limits of object type inference
See original GitHub issueHello,
I would simply like to split a store in different files and especially to make my different store parts (state, getter, actions) able to inherit from interface, abstract classes, etc…
Furthermore, splitting parts of a Pinia store seems to break the this
usage.
As Pinia seems to not really be typed (cause it is only based on object type inference), we just can’t split the different parts of a store.
I really wonder why Pinia didn’t do like vuex-smart-module allowing to have strong typing through classes usage and to use directly the type of each parts of a store without creating redundant Interface types. People easily say, “oh classes usage like in vuex-smart-module is more verbose cause you have to specify generics”, but when you have to use State, Getter or Action TYPE, you clearly don’t want to write/specify n interfaces with k methods for each part a store.
It is common for a lot of projects with strong architecture to have for requirement the need of an abstraction between different parts of different stores.
Example two instances of store like:
ProjectResourceStore
and HomeResourceStore
having each one Action inheriting from the same abstract class AResourceActions
.
What is really useful is that AResourceActions
allows having default implementations and specifying a “contract”.
ProjectResourceAction
and HomeResourceAction
will be obligated to implement all non-implemented AResourceActions methods.
What I say above seems to be clearly impossible with Pinia as it has only object type inference.
It’s also important to understand that with classes if I want to have a Type of a store part (State, Getter, Action), I don’t have to manually specify all the properties/methods in a redundant Interface that will be used as a way to have non-infered type. Indeed, with classes, I can directly use the class as a Type.
This problem has already been mentioned here: #343, but my issue just try to expose the problem with other related issues and limitations of Pinia type system. Also, no satisfying code example in the original issue has been provided.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:9
- Comments:46 (3 by maintainers)
Top GitHub Comments
Just a followup on this. The example provided by @rzym-on may run, but it fails under strict type checking. After a massive amount of sleuthing into the pinia types, I finally figured out how to split up a store into multiple files and preserve complete type checking. Here we go!
First you need to add this utility somewhere in your project. Let’s assume it’s in
@store/
:extractStore.ts
Now create a directory for the store. Let’s assume it’s
@store/foo
. These files go in the directory:state.ts
getters.ts
actions.ts
Now you can bring all of the pieces together like this:
Boom! Completely typed and nicely factored. You would then import it like this:
Enjoy! 😁
you can separate it simple
actions.ts
getters.ts
index.ts
state.ts
extractStore.ts
StoreInterFace.ts