Skip to content

Commit

Permalink
feat(types): add StorState, StoreGetters, and StoreActions helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Jul 28, 2021
1 parent d479370 commit 47c0610
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Expand Up @@ -3,6 +3,7 @@ export { createPinia } from './createPinia'
export type { Pinia, PiniaStorePlugin, PiniaPluginContext } from './rootStore'

export { defineStore } from './store'
export type { StoreActions, StoreGetters, StoreState } from './store'

export type {
StateTree,
Expand Down
40 changes: 40 additions & 0 deletions src/store.ts
Expand Up @@ -36,6 +36,7 @@ import {
DefineSetupStoreOptions,
DefineStoreOptionsInPlugin,
StoreGeneric,
StoreWithGetters,
} from './types'
import {
getActivePinia,
Expand Down Expand Up @@ -615,6 +616,45 @@ type _ExtractGettersFromSetupStore<SS> = _SpreadPropertiesFromObject<
ComputedRef<any>
>

/**
* Extract the actions of a store type. Works with both a Setup Store or an
* Options Store.
*/
export type StoreActions<SS> = SS extends Store<
string,
StateTree,
GettersTree<StateTree>,
infer A
>
? A
: _ExtractActionsFromSetupStore<SS>

/**
* Extract the getters of a store type. Works with both a Setup Store or an
* Options Store.
*/
export type StoreGetters<SS> = SS extends Store<
string,
StateTree,
infer G,
ActionsTree
>
? StoreWithGetters<G>
: _ExtractGettersFromSetupStore<SS>

/**
* Extract the state of a store type. Works with both a Setup Store or an
* Options Store. Note this unwraps refs.
*/
export type StoreState<SS> = SS extends Store<
string,
infer S,
GettersTree<StateTree>,
ActionsTree
>
? UnwrapRef<S>
: _ExtractStateFromSetupStore<SS>

// type a1 = _ExtractStateFromSetupStore<{ a: Ref<number>; action: () => void }>
// type a2 = _ExtractActionsFromSetupStore<{ a: Ref<number>; action: () => void }>
// type a3 = _ExtractGettersFromSetupStore<{
Expand Down
3 changes: 0 additions & 3 deletions test-dts/tsconfig.json
Expand Up @@ -3,9 +3,6 @@
"compilerOptions": {
"noEmit": true,
"declaration": true,
"paths": {
"vue-router": ["../dist"]
},
"noImplicitReturns": false
},
"include": ["./"],
Expand Down
69 changes: 69 additions & 0 deletions test-dts/typeHelpers.test-d.ts
@@ -0,0 +1,69 @@
import { StoreDefinition } from 'dist/pinia'
import { computed, ref } from 'vue'
import {
StoreState,
StoreGetters,
StoreActions,
defineStore,
expectType,
} from './'

const useSetupStore = defineStore('main', () => {
const n = ref(0)

const double = computed(() => n.value * 2)

function increment(amount = 1) {
n.value += amount
}

return { n, increment, double }
})

const useOptionsStore = defineStore('main', {
state: () => ({ n: 0 }),
getters: {
double: (state) => state.n * 2,
},
actions: {
increment(amount = 1) {
this.n += amount
},
},
})

declare function storeActions<T extends StoreDefinition>(
useStore: T
): StoreActions<ReturnType<T>>

declare function storeState<T extends StoreDefinition>(
useStore: T
): StoreState<ReturnType<T>>

declare function storeGetters<T extends StoreDefinition>(
useStore: T
): StoreGetters<ReturnType<T>>

expectType<{
increment: (amount?: number) => void
}>(storeActions(useSetupStore))

expectType<{ n: number }>(storeState(useSetupStore))

expectType<{ double: number }>(storeGetters(useSetupStore))

expectType<{
increment: (amount?: number) => void
}>(storeActions(useOptionsStore))

expectType<{ n: number }>(storeState(useOptionsStore))

expectType<{ double: number }>(storeGetters(useOptionsStore))

expectType<{ n: number }>(
storeState(
defineStore('', {
state: () => ({ n: ref(0) }),
})
)
)

0 comments on commit 47c0610

Please sign in to comment.