* @internal
*/
export type _MapStateReturn<S extends StateTree, G extends GettersTree<S>> = {
- [key in keyof S | keyof G]: () => Store<string, S, G, {}>[key]
+ // [key in keyof S | keyof G]: () => key extends keyof S
+ // ? S[key]
+ // : key extends keyof G
+ // ? G[key]
+ // : never
+ [key in keyof S | keyof G]: () => key extends keyof Store<string, S, G, {}>
+ ? Store<string, S, G, {}>[key]
+ : never
}
/**
> = {
[key in keyof T]: () => T[key] extends (store: Store) => infer R
? R
- : T[key] extends keyof S | keyof G
+ : T[key] extends keyof Store<Id, S, G, A>
? Store<Id, S, G, A>[T[key]]
: never
}
return Array.isArray(keysOrMapper)
? keysOrMapper.reduce((reduced, key) => {
reduced[key] = function (this: ComponentPublicInstance) {
+ // @ts-expect-error
return getCachedStore(this, useStore)[key]
} as () => any
return reduced
// function
return typeof storeKey === 'function'
? (storeKey as (store: Store<Id, S, G, A>) => any).call(this, store)
- : store[storeKey as keyof S | keyof G]
+ : store[storeKey as keyof typeof store]
}
return reduced
}, {} as _MapStateObjectReturn<Id, S, G, A, KeyMapper>)
* @internal
*/
export type _MapActionsReturn<A> = {
- [key in keyof A]: Store<string, StateTree, {}, A>[key]
+ [key in keyof A]: A[key]
}
/**
* @internal
*/
export type _MapActionsObjectReturn<A, T extends Record<string, keyof A>> = {
- [key in keyof T]: Store<string, StateTree, {}, A>[T[key]]
+ [key in keyof T]: A[T[key]]
}
/**
): _MapActionsReturn<A> | _MapActionsObjectReturn<A, KeyMapper> {
return Array.isArray(keysOrMapper)
? keysOrMapper.reduce((reduced, key) => {
+ // @ts-expect-error
reduced[key] = function (
this: ComponentPublicInstance,
...args: any[]
) {
+ // @ts-expect-error
return (getCachedStore(this, useStore)[key] as _Method)(...args)
- } as Store<string, StateTree, {}, A>[keyof A]
+ }
return reduced
}, {} as _MapActionsReturn<A>)
: Object.keys(keysOrMapper).reduce((reduced, key: keyof KeyMapper) => {
+ // @ts-expect-error
reduced[key] = function (
this: ComponentPublicInstance,
...args: any[]
) {
+ // @ts-expect-error
return getCachedStore(this, useStore)[keysOrMapper[key]](...args)
- } as Store<string, StateTree, {}, A>[keyof KeyMapper[]]
+ }
return reduced
}, {} as _MapActionsObjectReturn<A, KeyMapper>)
}
*/
export type _MapWritableStateReturn<S extends StateTree> = {
[key in keyof S]: {
- get: () => Store<string, S, {}, {}>[key]
- set: (value: Store<string, S, {}, {}>[key]) => any
+ get: () => S[key]
+ set: (value: S[key]) => any
}
}
T extends Record<string, keyof S>
> = {
[key in keyof T]: {
- get: () => Store<string, S, {}, {}>[T[key]]
- set: (value: Store<string, S, {}, {}>[T[key]]) => any
+ get: () => S[T[key]]
+ set: (value: S[T[key]]) => any
}
}
// @ts-ignore
reduced[key] = {
get(this: ComponentPublicInstance) {
+ // @ts-expect-error
return getCachedStore(this, useStore)[key]
},
set(this: ComponentPublicInstance, value) {
// it's easier to type it here as any
+ // @ts-expect-error
return (getCachedStore(this, useStore)[key] = value as any)
},
}
// @ts-ignore
reduced[key] = {
get(this: ComponentPublicInstance) {
+ // @ts-expect-error
return getCachedStore(this, useStore)[keysOrMapper[key]]
},
set(this: ComponentPublicInstance, value) {
// it's easier to type it here as any
+ // @ts-expect-error
return (getCachedStore(this, useStore)[keysOrMapper[key]] =
value as any)
},
/**
* State of the Store. Setting it will replace the whole state.
*/
- $state: UnwrapRef<S> & PiniaCustomStateProperties<S>
+ $state: (StateTree extends S ? {} : UnwrapRef<S>) &
+ PiniaCustomStateProperties<S>
/**
* Private property defining the pinia the store is attached to.
// has the actions without the context (this) for typings
A /* extends ActionsTree */ = ActionsTree
> = StoreWithState<Id, S, G, A> &
- UnwrapRef<S> &
- StoreWithGetters<G> &
- StoreWithActions<A> &
+ (StateTree extends S ? {} : UnwrapRef<S>) &
+ (GettersTree<S> extends G ? {} : StoreWithGetters<G>) &
+ (ActionsTree extends A ? {} : StoreWithActions<A>) &
PiniaCustomProperties<Id, S, G, A> &
PiniaCustomStateProperties<S>
+/**
+ * Generic version of Store. Doesn't fail on access with strings
+ */
+export type GenericStore = StoreWithState<
+ string,
+ StateTree,
+ GettersTree<StateTree>,
+ ActionsTree
+> &
+ PiniaCustomProperties<
+ string,
+ StateTree,
+ GettersTree<StateTree>,
+ ActionsTree
+ > &
+ PiniaCustomStateProperties<StateTree>
+
/**
* Return type of `defineStore()`. Function that allows instantiating a store.
*/
$id: Id
}
-/**
- * Generic version of Store.
- * @deprecated Use Store instead
- */
-export type GenericStore = Store
-
/**
* Properties that are added to every store by `pinia.use()`
*/