import { InjectionKey } from "vue" ; import { Store as BaseStore , createStore as baseCreateStore , useStore as baseUseStore , ModuleTree , Plugin , StoreOptions as OriginalStoreOptions , GetterTree as OriginalGetterTree , ActionTree as OriginalActionTree , MutationTree as OriginalMutationTree , } from "vuex" ; export type PayloadFunction = ( payload ? : any ) => any ; export type GettersBase = Record < string , any > ; export type ActionsBase = Record < string , PayloadFunction > ; export type MutationsBase = Record < string , any > ; export type PromiseType < T > = T extends Promise < infer P > ? P : T ; export class Store < S , G extends GettersBase , A extends ActionsBase , M extends MutationsBase > extends BaseStore < S > { constructor ( options ) { super ( options ) ; } readonly getters ! : G ; dispatch : Dispatch < A > ; commit : Commit < M > ; } export function createStore < S , G extends GettersBase , A extends ActionsBase , M extends MutationsBase > ( options ) { return baseCreateStore < S > ( options as OriginalStoreOptions < S > ) as Store < S , G , A , M > ; } export function useStore < S , G extends GettersBase , A extends ActionsBase , M extends MutationsBase > ( injectKey ? ) { return baseUseStore < S > ( injectKey ) as Store < S , G , A , M > ; } export interface Dispatch < A extends ActionsBase > { < T extends keyof A > ( type , ... payload ) : Promise < PromiseType < ReturnType < A [ T ] > > > ; < T extends keyof A > ( payloadWithType ) : Promise < PromiseType < ReturnType < A [ T ] > > > ; } export interface Commit < M extends MutationsBase > { < T extends keyof M > ( type , ... payload ) : void ; < T extends keyof M > ( payloadWithType ) : void ; } export interface StoreOptions < S , G extends GettersBase , A extends ActionsBase , M extends MutationsBase , SG extends GettersBase = G , SA extends ActionsBase = A , SM extends MutationsBase = M > { state ? : S | ( ( ) => S ) ; getters : GetterTree < S , S , G , SG > ; actions : ActionTree < S , S , A , SG , SA , SM > ; mutations : MutationTree < S , M > ; modules ? : ModuleTree < S > ; plugins ? : Plugin < S > [ ] ; strict ? : boolean ; devtools ? : boolean ; } export interface ActionContext < S , R , SG extends GettersBase , SA extends ActionsBase , SM extends MutationsBase > { dispatch : Dispatch < SA > ; commit : Commit < SM > ; state : S ; getters : SG ; rootState : R ; rootGetters : any ; } export type ActionHandler < S , R , SG extends GettersBase , SA extends ActionsBase , SM extends MutationsBase , K extends keyof SA > = ( this : Store < S , SG , SA , SM > , injectee : ActionContext < S , R , SG , SA , SM > , payload : Parameters < SA [ K ] > [ 0 ] ) => ReturnType < SA [ K ] > ; export interface ActionObject < S , R , SG extends GettersBase , SA extends ActionsBase , SM extends MutationsBase , K extends keyof SA > { root ? : boolean ; handler : ActionHandler < S , R , SG , SA , SM , K > ; } export type Getter < S , R , G , K extends keyof G , SG extends GettersBase > = ( state : S , getters : SG , rootState : R , rootGetters : any ) => SG [ K ] ; export type Action < S , R , A extends ActionsBase , K extends keyof A , SG extends GettersBase , SA extends ActionsBase , SM extends MutationsBase > = ActionHandler < S , R , SG , SA , SM , K > | ActionObject < S , R , SG , SA , SM , K > ; export type Mutation < S , P > = ( state : S , payload : P ) => void ; export type GetterTree < S , R , G , SG = G > = G extends GettersBase ? CustomGetterTree < S , R , G , SG > : OriginalGetterTree < S , R > ; export type CustomGetterTree < S , R , G extends GettersBase , SG extends GettersBase > = { [ K in keyof G ] : Getter < S , R , G , K , SG > ; } ; export type ActionTree < S , R , A , SG , SA , SM > = A extends ActionsBase ? SA extends ActionsBase ? CustomActionTree < S , R , A , SG , SA , SM > : OriginalActionTree < S , R > : OriginalActionTree < S , R > ; export type CustomActionTree < S , R , A extends ActionsBase , SG extends GettersBase , SA extends ActionsBase , SM extends MutationsBase > = { [ K in keyof A ] : Action < S , R , A , K , SG , SA , SM > ; } ; export type MutationTree < S , M > = M extends MutationsBase ? CustomMutationTree < S , M > : OriginalMutationTree < S > ; export type CustomMutationTree < S , M extends MutationsBase > = { [ K in keyof M ] : Mutation < S , M [ K ] > ; } ;