From 859eeb3b348bed07faa8583c46d1747f9362f213 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Mon, 20 Jan 2020 19:14:09 +0100 Subject: [PATCH] feat: allow using getters in other getters --- __tests__/getters.spec.ts | 7 +++++++ src/store.ts | 2 +- src/types.ts | 10 ++-------- 3 files changed, 10 insertions(+), 9 deletions(-) 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> -- 2.47.2