From: Eduardo San Martin Morote Date: Mon, 20 Jan 2020 18:14:09 +0000 (+0100) Subject: feat: allow using getters in other getters X-Git-Tag: 0.0.5~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=859eeb3b348bed07faa8583c46d1747f9362f213;p=thirdparty%2Fvuejs%2Fpinia.git feat: allow using getters in other getters --- diff --git a/__tests__/getters.spec.ts b/__tests__/getters.spec.ts index 3919ce24..9a56fcc2 100644 --- a/__tests__/getters.spec.ts +++ b/__tests__/getters.spec.ts @@ -11,6 +11,8 @@ describe('Store', () => { }), getters: { upperCaseName: ({ name }) => name.toUpperCase(), + composed: (state, { upperCaseName }) => + (upperCaseName.value as string) + ': ok', }, })() } @@ -58,4 +60,9 @@ describe('Store', () => { aStore.state.a = 'b' expect(aStore.fromB.value).toBe('b b') }) + + it('can use other getters', () => { + const store = useStore() + expect(store.composed.value).toBe('EDUARDO: ok') + }) }) diff --git a/src/store.ts b/src/store.ts index 9bd3329d..83c86d8e 100644 --- a/src/store.ts +++ b/src/store.ts @@ -121,7 +121,7 @@ export function buildStore< computedGetters[getterName] = computed(() => { setActiveReq(_r) // eslint-disable-next-line @typescript-eslint/no-use-before-define - return getters[getterName](state.value) + return getters[getterName](state.value, computedGetters) }) as StoreWithGetters[typeof getterName] } diff --git a/src/types.ts b/src/types.ts index 8a59e111..72570438 100644 --- a/src/types.ts +++ b/src/types.ts @@ -17,7 +17,8 @@ export function isPlainObject( export type NonNullObject = Record export interface StoreGetter { - (state: S): T + // TODO: would be nice to be able to define the getters here + (state: S, getters: Record>): T } type TODO = any @@ -30,13 +31,6 @@ export type SubscriptionCallback = ( state: S ) => void -export type StoreReactiveGetters< - S extends StateTree, - G extends Record any> -> = { - [k in keyof G]: G[k] extends (state: S, getters: any) => infer V ? V : never -} - export type StoreWithGetters< S extends StateTree, G extends Record>