-import { defineStore } from '../../src'
+import { defineStore, mapStores, Store } from '../../src'
import { expectType, expectError } from 'tsd'
+import { StoreDefinition } from 'src/types'
const useStore = defineStore({
id: 'name',
},
})
+const useCounter = defineStore({
+ id: 'counter',
+ state: () => ({ n: 0 }),
+})
+
+const useStoreDos = defineStore({
+ id: 'dos',
+ state: () => ({}),
+})
+
const store = useStore()
expectType<{ a: 'on' | 'off' }>(store.$state)
+expectType<string>(store.upper)
expectType<{ upper: string }>(store)
expectError(() => store.nonExistant)
+
+type MainStore = ReturnType<typeof useStore>
+type DosStore = ReturnType<typeof useStoreDos>
+type CounterStore = ReturnType<typeof useCounter>
+
+expectType<{
+ name: () => MainStore
+ dos: () => DosStore
+ counter: () => CounterStore
+}>(mapStores(useStore, useStoreDos, useCounter))
PiniaCustomProperties,
} from './types'
+export { mapStores } from './mapHelpers'
// TODO: remove in beta
export { createStore } from './deprecated'
Method,
StateDescriptor,
PiniaCustomProperties,
+ StoreDefinition,
} from './types'
import { useStoreDevtools } from './devtools'
import {
StoreWithGetters<G> &
PiniaCustomProperties
>
-}) {
+}): StoreDefinition<Id, S, G, A> {
const { id, state, getters, actions } = options
- return function useStore(pinia?: Pinia | null): Store<Id, S, G, A> {
+ function useStore(pinia?: Pinia | null): Store<Id, S, G, A> {
// const vm = getCurrentInstance()
// pinia = pinia || (vm && ((vm as any).$pinia as Pinia))
pinia = pinia || (getCurrentInstance() && inject(piniaSymbol))
actions as Record<string, Method> | undefined
)
}
+
+ // used by devtools
+ useStore.$id = id
+
+ return useStore
}
StoreWithActions<A> &
PiniaCustomProperties<Id, S, G, A>
+/**
+ * Return type of `defineStore()`. Function that allows instantiating a store.
+ */
+export interface StoreDefinition<
+ Id extends string,
+ S extends StateTree,
+ G /* extends Record<string, StoreGetterThis> */,
+ A /* extends Record<string, StoreAction> */
+> {
+ (pinia?: Pinia | null | undefined): Store<Id, S, G, A>
+ $id: Id
+}
+
+/**
+ * Generic version of Store
+ */
export type GenericStore = Store<
string,
StateTree,
Record<string, Method>
>
+/**
+ * Generic version of `StoreDefinition`
+ */
+export interface GenericStoreDefinition {
+ (pinia?: Pinia | null | undefined): Store<
+ string,
+ StateTree,
+ Record<string, Method>,
+ Record<string, Method>
+ >
+ $id: string
+}
+
export interface DevtoolHook {
on(
event: string,