StoreOnActionListenerContext,
SubscriptionCallback,
PiniaCustomProperties,
+ PiniaCustomStateProperties,
DefineStoreOptions,
} from './types'
export { MutationType } from './types'
Store,
DefineStoreOptions,
ActionsTree,
+ PiniaCustomStateProperties,
} from './types'
import type Vue from 'vue'
*
* @param context - Context
*/
- (context: PiniaPluginContext): Partial<PiniaCustomProperties> | void
+ (context: PiniaPluginContext): Partial<
+ PiniaCustomProperties & PiniaCustomStateProperties
+ > | void
}
/**
/**
* State of the Store. Setting it will replace the whole state.
*/
- $state: UnwrapRef<S>
+ $state: UnwrapRef<S> & PiniaCustomStateProperties<S>
/**
* Private property defining the pinia the store is attached to.
UnwrapRef<S> &
StoreWithGetters<G> &
StoreWithActions<A> &
- PiniaCustomProperties<Id, S, G, A>
+ PiniaCustomProperties<Id, S, G, A> &
+ PiniaCustomStateProperties<S>
// TODO: check if it's possible to add = to StoreDefinition and Store and cleanup GenericStore and the other one
A = ActionsTree
> {}
+/**
+ * Properties that are added to every `store.$state` by `pinia.use()`
+ */
+export interface PiniaCustomStateProperties<S extends StateTree = StateTree> {}
+
/**
* Type of an object of Getters that infers the argument
*
*/
export type GettersTree<S extends StateTree> = Record<
string,
- ((state: UnwrapRef<S>) => any) | (() => any)
+ ((state: UnwrapRef<S & PiniaCustomStateProperties<S>>) => any) | (() => any)
>
/**
* Optional object of getters.
*/
getters?: G &
- ThisType<UnwrapRef<S> & StoreWithGetters<G> & PiniaCustomProperties>
+ ThisType<
+ UnwrapRef<S> &
+ StoreWithGetters<G> &
+ PiniaCustomProperties &
+ PiniaCustomStateProperties
+ >
/**
* Optional object of actions.
*/
UnwrapRef<S> &
StoreWithState<Id, S> &
StoreWithGetters<G> &
- PiniaCustomProperties
+ PiniaCustomProperties &
+ PiniaCustomStateProperties
>
}
$actions: Array<keyof A>
}
+ export interface PiniaCustomStateProperties<S> {
+ myState: number
+ }
+
export interface DefineStoreOptions<Id, S, G, A> {
debounce?: {
// Record<keyof A, number>
expectType<string>(context.options.id)
expectType<string>(context.store.$id)
+ expectType<number>(context.store.$state.myState)
+ expectType<number>(context.store.myState)
+
return {
$actions: Object.keys(context.options.actions || {}),
}
const useStore = defineStore({
id: 'main',
+ state: () => ({}),
actions: {
one() {},
two() {
this.one()
+ expectType<number>(this.$state.myState)
+ expectType<number>(this.myState)
},
three() {
this.two()
},
})
+expectType<{ myState: number }>(useStore().$state)
+
type Procedure = (...args: any[]) => any
function debounce<F extends Procedure>(fn: F, time = 200) {