Client - extend store typing in components

This commit is contained in:
Sam
2021-08-11 22:12:02 +02:00
parent a20a646687
commit 72db7afe44
10 changed files with 93 additions and 15 deletions

View File

@ -1,5 +1,6 @@
export enum RootGetters {
ERROR_MESSAGE = 'ERROR_MESSAGE',
LANGUAGE = 'LANGUAGE',
}
export enum RootMutations {

View File

@ -6,4 +6,7 @@ export const getters: GetterTree<IRootState, IRootState> & IRootGetters = {
[ROOT_STORE.GETTERS.ERROR_MESSAGE]: (state: IRootState) => {
return state.errorMessage
},
[ROOT_STORE.GETTERS.LANGUAGE]: (state: IRootState) => {
return state.language
},
}

View File

@ -8,4 +8,5 @@ export interface IRootState {
export interface IRootGetters {
[ROOT_STORE.GETTERS.ERROR_MESSAGE](state: IRootState): string | null
[ROOT_STORE.GETTERS.LANGUAGE](state: IRootState): string
}

View File

@ -1,8 +1,28 @@
import { Store as VuexStore, CommitOptions } from 'vuex'
import { ROOT_STORE } from '@/store/constants'
import { IRootState } from '@/store/modules/root/interfaces'
import { IRootGetters, IRootState } from '@/store/modules/root/interfaces'
export type TRootMutations<S = IRootState> = {
[ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGE](state: S): void
[ROOT_STORE.MUTATIONS.SET_ERROR_MESSAGE](state: S, errorMessage: string): void
[ROOT_STORE.MUTATIONS.UPDATE_LANG](state: S, language: string): void
}
export type TRootStoreModule<S = IRootState> = Omit<
VuexStore<S>,
'commit' | 'getters' | 'dispatch'
> & {
getters: {
[K in keyof IRootGetters]: ReturnType<IRootGetters[K]>
}
} & {
commit<
K extends keyof TRootMutations,
P extends Parameters<TRootMutations[K]>[1]
>(
key: K,
payload?: P,
options?: CommitOptions
): ReturnType<TRootMutations[K]>
}

View File

@ -1,6 +1,36 @@
import { Store as VuexStore, CommitOptions, DispatchOptions } from 'vuex'
import { USER_STORE } from '@/store/constants'
import { IUserState } from '@/store/modules/user/interfaces'
import {
IUserActions,
IUserGetters,
IUserState,
} from '@/store/modules/user/interfaces'
export type TUserMutations<S = IUserState> = {
[USER_STORE.MUTATIONS.UPDATE_AUTH_TOKEN](state: S, authToken: string): void
}
export type TUserStoreModule<S = IUserState> = Omit<
VuexStore<S>,
'commit' | 'getters' | 'dispatch'
> & {
dispatch<K extends keyof IUserActions>(
key: K,
payload?: Parameters<IUserActions[K]>[1],
options?: DispatchOptions
): ReturnType<IUserActions[K]>
} & {
getters: {
[K in keyof IUserGetters]: ReturnType<IUserGetters[K]>
}
} & {
commit<
K extends keyof TUserMutations,
P extends Parameters<TUserMutations[K]>[1]
>(
key: K,
payload?: P,
options?: CommitOptions
): ReturnType<TUserMutations[K]>
}

View File

@ -0,0 +1,10 @@
import { TRootStoreModule } from '@/store/modules/root/types'
import { TUserStoreModule } from '@/store/modules/user/types'
type StoreModules = {
rootModule: TRootStoreModule
userModule: TUserStoreModule
}
export type Store = TUserStoreModule<Pick<StoreModules, 'userModule'>> &
TRootStoreModule<Pick<StoreModules, 'rootModule'>>