Return Type of defineStore with TypeScript #1277
-
I´m trying to figure out how to pass a store as a parameter inside my application. And I´m failing so far. I´m using Vue3 with Pinia and TypeScript. My IDE is JetBrains PhpStorm btw. My User store looks like this.
No problems so far. It can be used in my Vue components in the setup method. Even in functions like this:
This works. I get type hinting and everything works fine. Inspecting the type gives me the following:
As soon as I move that function to its own file the troubles start. The type of my User Store is using generics from pinia.d.ts and the Store type. If I understand this correctly there is no written type definition because of the generics. What I want to achieve is dependency injection for a class defined in its own file.
This class would be instantiated inside a Vue component and the store injected from there. I can´t figure out the type here. As a solution I wrote an interface and use it as the type in the constructor
The TypeScript compiler does not like my idea:
Same goes for trying to cast the userStore to the UserStoreMethods interface. "No overlapping types" it goes. My workaround is:
With this I´m able to get auto completion for the types. I could live with that, but it´s a hacky work around. There is a second work around. Not injecting the store as a parameter but simply calling It feels like I´m working against the framework here. It seems to me the idea from Pinia is to use Can anyone point me in the right direction? Or is it really: working against the framework and not a good idea to pass stores around as parameters? Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You can use the type ReturnType |
Beta Was this translation helpful? Give feedback.
You can use the type ReturnType
#1054 (comment)