Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

separating getters and actions to dedicated files break typing of this #343

Closed
yaquawa opened this issue Jan 26, 2021 · 7 comments
Closed

Comments

@yaquawa
Copy link

yaquawa commented Jan 26, 2021

I want to separate my getters and actions to dedicated files instead of just put them all together in one store file, like this:

import { defineStore } from 'pinia'
import { state } from './state'
import { getters } from './getters'
import { actions } from './actions'

export const useMainStore = defineStore({
  id: 'main',
  state: () => state,
  getters,
  actions,
})

But in the getters and actions file, I can not use this to reference the store object now.
What should I do for such a situation?
Thanks!

@posva
Copy link
Member

posva commented Jan 26, 2021

You have to put the getters and actions inlined on the defineStore to infer typings. I don't think there is a way to manually type this in getters and actions unless you manually create the types for state, getters, and actions

@posva posva closed this as completed Jan 26, 2021
@yaquawa
Copy link
Author

yaquawa commented Jan 26, 2021

@posva So the only thing I could do is breaking a gigantic store into small ones?
It'll be helpful if you can give some best practices for how to organize big store(maybe have this topic on the README).
For example should I split them by feature or role or any other category?

Thanks!

@posva
Copy link
Member

posva commented Jan 26, 2021

You will have to manually type this using the Store type and creating types for each of its generics
https://github.com/posva/pinia/blob/fc56acb41e3753ddc8aef965d3d78bfac4f93db5/src/store.ts#L216

Some of them can be inferred with ReturnType<typeof state> but you will need to manually do G and A for getters and actions

@posva
Copy link
Member

posva commented Jan 26, 2021

you can also split a store into multiple stores

@vycoder
Copy link

vycoder commented Jul 25, 2021

Hi @posva, if it's not too much to ask, can you provide an example (or even include it in the documentation) of how to properly type this when splitting them into their own separate files?

@aparajita
Copy link

aparajita commented Jan 21, 2022

@yaquawa @vycoder , see here for an example of how to split up a store.

@theodinspire
Copy link

I was able to get this done this way:

type Getters = {
	[Property in keyof typeof getters]: ReturnType<typeof getters[Property]>
} // Maps your getters to their return types

type Actions { ... }; // The type definition for your actions
export const actions: Actions & ThisType<typeof defaultState & Getters & Actions> = { ... };

defineStore('id', { state: () => defaultState, getters, actions });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants