* @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
}
}
? keysOrMapper.reduce((reduced, key) => {
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)
},
}
: Object.keys(keysOrMapper).reduce((reduced, key: keyof KeyMapper) => {
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)
},
Id extends string,
S extends StateTree,
G extends GettersTree<S>,
- A /* extends Record<string, StoreAction> */
+ A /* extends ActionsTree */
>(options: DefineStoreOptions<Id, S, G, A>): StoreDefinition<Id, S, G, A> {
const { id, state, getters, actions } = options
storeAndDescriptor[0],
storeAndDescriptor[1],
id,
- getters as GettersTree<S> | undefined,
- actions as ActionsTree | undefined,
- // @ts-expect-error: because of the extend on Actions
+ getters,
+ // @ts-expect-error: all good
+ actions,
options
)
provide(storeAndDescriptor[2], store)
}
- return store
+ return store as Store<Id, S, G, A>
}
return (
(hasInstance && inject(storeAndDescriptor[2], null)) ||
- buildStoreToUse(
+ (buildStoreToUse(
storeAndDescriptor[0],
storeAndDescriptor[1],
id,
- getters as GettersTree<S> | undefined,
- actions as ActionsTree | undefined,
- // @ts-expect-error: because of the extend on Actions
+ getters,
+ // @ts-expect-error: all good
+ actions,
options
- )
+ ) as Store<Id, S, G, A>)
)
}