vuex-typescript-interface
vuex-typescript-interface copied to clipboard
Getters with arguments
This is a fantastic project - thank you for making it!
Vuex gives the ability to use getters with method style access. That's useful to have a getter that will pluck a specific element from an array based on the passed parameter for example.
I don't see any way of doing that here. Perhaps one way of doing this would be to add an args array to the getter signature, after the root getters? Not sure how it would be possible to type that since getters don't take arguments though.
An alternative option for this sort of thing (a getter with a parameter) is to use plugins, but that doesn't appear to support typing here?
I've found that I can typescript define a getter with an argument in the viex-typescript-interface interface using:
readonly data: (id: number) => IData;
Then in the getters:
data: (state) => {
return (id) => state.data.find(dp => dp.id == id);
},
So this can probably be safely closed and ignored... :-). That said, I wonder about the plug-in part?
Glad you figured it out!
Thanks for bringing up plugins, I'll have to create a createPlugin<RootStore>(...) function - it will only pass back the function given - but it will give all type information to the plugin. Thank you!