From: Eduardo San Martin Morote Date: Wed, 31 Mar 2021 10:05:12 +0000 (+0200) Subject: fix(types): pass custom properties to actions and getters X-Git-Tag: v0.2.3~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=6a5326f3acd6e1244401bf276964c348e6cb8721;p=thirdparty%2Fvuejs%2Fpinia.git fix(types): pass custom properties to actions and getters --- diff --git a/__tests__/storePlugins.spec.ts b/__tests__/storePlugins.spec.ts index 62bb11ee..6d6f31f5 100644 --- a/__tests__/storePlugins.spec.ts +++ b/__tests__/storePlugins.spec.ts @@ -11,7 +11,21 @@ declare module '../src' { } describe('store plugins', () => { - const useStore = defineStore({ id: 'test' }) + const useStore = defineStore({ + id: 'test', + + actions: { + incrementN() { + return this.n++ + }, + }, + + getters: { + doubleN() { + return this.n * 2 + }, + }, + }) const localVue = createLocalVue() localVue.use(PiniaPlugin) @@ -48,4 +62,33 @@ describe('store plugins', () => { expect(store.n).toBe(1) expect(store.hasPinia).toBe(true) }) + + it('can be used in actions', () => { + const pinia = createPinia() + pinia.Vue = Vue + mount({ template: '

' }, { localVue }) + + // must call use after installing the plugin + pinia.use(() => { + return { n: 20 } + }) + + const store = useStore(pinia) + + expect(store.incrementN()).toBe(20) + }) + + it('can be used in getters', () => { + const pinia = createPinia() + pinia.Vue = Vue + mount({ template: '

' }, { localVue }) + + // must call use after installing the plugin + pinia.use(() => { + return { n: 20 } + }) + + const store = useStore(pinia) + expect(store.doubleN).toBe(40) + }) }) diff --git a/src/store.ts b/src/store.ts index 61ceedf9..61c4aeef 100644 --- a/src/store.ts +++ b/src/store.ts @@ -18,6 +18,7 @@ import { StoreWithActions, Method, StateDescriptor, + PiniaCustomProperties, } from './types' import { useStoreDevtools } from './devtools' import { @@ -25,7 +26,6 @@ import { Pinia, setActivePinia, getActivePinia, - PiniaCustomProperties, piniaSymbol, } from './rootStore' import { assign } from './utils' @@ -253,9 +253,16 @@ export function defineStore< >(options: { id: Id state?: () => S - getters?: G & ThisType> + getters?: G & ThisType & PiniaCustomProperties> // allow actions use other actions - actions?: A & ThisType & StoreWithGetters> + actions?: A & + ThisType< + A & + S & + StoreWithState & + StoreWithGetters & + PiniaCustomProperties + > }) { const { id, state, getters, actions } = options