Skip to content

Commit 859eeb3

Browse files
committed
feat: allow using getters in other getters
1 parent a1d2790 commit 859eeb3

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

__tests__/getters.spec.ts

+7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ describe('Store', () => {
1111
}),
1212
getters: {
1313
upperCaseName: ({ name }) => name.toUpperCase(),
14+
composed: (state, { upperCaseName }) =>
15+
(upperCaseName.value as string) + ': ok',
1416
},
1517
})()
1618
}
@@ -58,4 +60,9 @@ describe('Store', () => {
5860
aStore.state.a = 'b'
5961
expect(aStore.fromB.value).toBe('b b')
6062
})
63+
64+
it('can use other getters', () => {
65+
const store = useStore()
66+
expect(store.composed.value).toBe('EDUARDO: ok')
67+
})
6168
})

src/store.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export function buildStore<
121121
computedGetters[getterName] = computed(() => {
122122
setActiveReq(_r)
123123
// eslint-disable-next-line @typescript-eslint/no-use-before-define
124-
return getters[getterName](state.value)
124+
return getters[getterName](state.value, computedGetters)
125125
}) as StoreWithGetters<S, G>[typeof getterName]
126126
}
127127

src/types.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ export function isPlainObject(
1717
export type NonNullObject = Record<any, any>
1818

1919
export interface StoreGetter<S extends StateTree, T = any> {
20-
(state: S): T
20+
// TODO: would be nice to be able to define the getters here
21+
(state: S, getters: Record<string, Ref<any>>): T
2122
}
2223

2324
type TODO = any
@@ -30,13 +31,6 @@ export type SubscriptionCallback<S> = (
3031
state: S
3132
) => void
3233

33-
export type StoreReactiveGetters<
34-
S extends StateTree,
35-
G extends Record<string, (state: S, getters: any) => any>
36-
> = {
37-
[k in keyof G]: G[k] extends (state: S, getters: any) => infer V ? V : never
38-
}
39-
4034
export type StoreWithGetters<
4135
S extends StateTree,
4236
G extends Record<string, StoreGetter<S>>

0 commit comments

Comments
 (0)