export type { Pinia, PiniaStorePlugin, PiniaPluginContext } from './rootStore'
export { defineStore } from './store'
+export type { StoreActions, StoreGetters, StoreState } from './store'
export type {
StateTree,
DefineSetupStoreOptions,
DefineStoreOptionsInPlugin,
StoreGeneric,
+ StoreWithGetters,
} from './types'
import {
getActivePinia,
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<{
--- /dev/null
+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) }),
+ })
+ )
+)