if (pinia) setActivePinia(pinia)
// TODO: worth warning on server if no piniaKey as it can leak data
pinia = getActivePinia()
- let stores = storesMap.get(pinia)
- if (!stores) storesMap.set(pinia, (stores = new Map()))
+ let storeCache = storesMap.get(pinia)
+ if (!storeCache) storesMap.set(pinia, (storeCache = new Map()))
- let storeAndDescriptor = stores.get(id) as
+ let storeAndDescriptor = storeCache.get(id) as
| [
StoreWithState<Id, S, G, A>,
StateDescriptor<S>,
storeAndDescriptor = initStore(id, state, pinia.state.value[id])
// @ts-expect-error: annoying to type
- stores.set(id, storeAndDescriptor)
+ storeCache.set(id, storeAndDescriptor)
store = buildStoreToUse<
Id,
/**
* State of the Store. Setting it will replace the whole state.
*/
- $state: UnwrapRef<S> & PiniaCustomStateProperties<S>
+ $state: UnwrapRef<StateTree extends S ? {} : S> &
+ PiniaCustomStateProperties<S>
/**
* Private property defining the pinia the store is attached to.
*/
export type GenericStore<
Id extends string = string,
- S extends StateTree = StateTree,
+ S extends StateTree = any,
G extends GettersTree<S> = GettersTree<S>,
// has the actions without the context (this) for typings
A /* extends ActionsTree */ = ActionsTree
*/
export type GettersTree<S extends StateTree> = Record<
string,
- ((state: UnwrapRef<S & PiniaCustomStateProperties<S>>) => any) | (() => any)
+ | ((
+ state: UnwrapRef<
+ (StateTree extends S ? {} : S) & PiniaCustomStateProperties<S>
+ >
+ ) => any)
+ | (() => any)
>
/**
* Unique string key to identify the store across the application.
*/
id: Id
+
/**
* Function to create a fresh state.
*/
state?: () => S
+
/**
* Optional object of getters.
*/
getters?: G &
- ThisType<UnwrapRef<S> & StoreWithGetters<G> & PiniaCustomProperties>
+ ThisType<
+ UnwrapRef<StateTree extends S ? {} : S> &
+ StoreWithGetters<G> &
+ PiniaCustomProperties
+ >
/**
* Optional object of actions.
*/
actions?: A &
ThisType<
A &
- UnwrapRef<S> &
+ UnwrapRef<StateTree extends S ? {} : S> &
StoreWithState<Id, S, G, A> &
- StoreWithGetters<G> &
+ StoreWithGetters<GettersTree<S> extends G ? {} : G> &
PiniaCustomProperties
>
}
},
actions: {
doStuff() {
+ // @ts-expect-error
+ this.notExisting
expectType<string>(this.upper)
expectType<false>(this.other)
},
},
})
+// actions on not existing properties
+defineStore({
+ id: '',
+ actions: {
+ a() {
+ // @ts-expect-error
+ this.notExisting
+ },
+ },
+})
+
+defineStore({
+ id: '',
+ state: () => ({}),
+ actions: {
+ a() {
+ // @ts-expect-error
+ this.notExisting
+ },
+ },
+})
+
+defineStore({
+ id: '',
+ getters: {},
+ actions: {
+ a() {
+ // @ts-expect-error
+ this.notExisting
+ },
+ },
+})
+
+// getters on not existing properties
+defineStore({
+ id: '',
+ getters: {
+ a(): number {
+ // @ts-expect-error
+ this.notExisting
+ return 2
+ },
+ b: (state) => {
+ // @ts-expect-error
+ state.notExisting
+ return
+ },
+ },
+})
+
+defineStore({
+ id: '',
+ state: () => ({}),
+ getters: {
+ a(): number {
+ // @ts-expect-error
+ this.notExisting
+ return 2
+ },
+ b: (state) => {
+ // @ts-expect-error
+ state.notExisting
+ return
+ },
+ },
+})
+
const store = useStore()
expectType<{ a: 'on' | 'off' }>(store.$state)
expectType<any>(genericStore.$state.thing)
takeStore(genericStore)
useSyncValueToStore(() => 2, genericStore, 'myState')
-useSyncValueToStore(() => 2, genericStore, 'random')
-// @ts-expect-error
useSyncValueToStore(() => false, genericStore, 'myState')
+useSyncValueToStore(() => 2, genericStore, 'random')