| 
1 | 1 | import {  | 
2 | 2 |     ActionContext, ActionTree, CommitOptions, DispatchOptions, GetterTree, MutationTree, Store  | 
3 |  | -} from 'vuex';  | 
 | 3 | +} from "vuex";  | 
 | 4 | + | 
 | 5 | +type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((  | 
 | 6 | +  k: infer I  | 
 | 7 | +) => void)  | 
 | 8 | +  ? I  | 
 | 9 | +  : never;  | 
4 | 10 | 
 
  | 
5 | 11 | type InferPayload<T> = T extends (_: any, payload?: any, options?: any) => any  | 
6 | 12 |   ? Parameters<T>[1]  | 
7 | 13 |   : never;  | 
8 | 14 | 
 
  | 
9 |  | -type InferMutation<T> = T extends undefined | null  | 
 | 15 | +type Mutation<T> = T extends undefined  | 
10 | 16 |   ? (() => void) & ((payload: undefined, options?: CommitOptions) => void)  | 
11 | 17 |   : (payload: T, options?: CommitOptions) => void;  | 
12 |  | -type InferMutations<T> = { [Key in keyof T]: InferMutation<InferPayload<T[Key]>> };  | 
13 | 18 | 
 
  | 
14 |  | -type InferAction<T, R> = T extends undefined | null  | 
 | 19 | +type InferMutation<T> = UnionToIntersection<Mutation<InferPayload<T>>>;  | 
 | 20 | +type InferMutations<T> = { [Key in keyof T]: InferMutation<T[Key]> };  | 
 | 21 | + | 
 | 22 | +type Action<T, R> = T extends undefined  | 
15 | 23 |   ? (() => R) & ((payload: undefined, options?: DispatchOptions) => R)  | 
16 | 24 |   : (payload: T, options?: DispatchOptions) => R;  | 
 | 25 | +type ActionReturn<T> = T extends (...args: any[]) => infer R ? R : void;  | 
17 | 26 | 
 
  | 
18 |  | -type InferActionReturn<T> = T extends (...args: any[]) => any ? ReturnType<T> : void;  | 
 | 27 | +type InferAction<T> = UnionToIntersection<Action<InferPayload<T>, ActionReturn<T>>>;  | 
 | 28 | +type InferActions<T> = { [Key in keyof T]: InferAction<T[Key]> };  | 
19 | 29 | 
 
  | 
20 |  | -type InferActions<T> = {  | 
21 |  | -  [Key in keyof T]: InferAction<InferPayload<T[Key]>, InferActionReturn<T[Key]>>  | 
 | 30 | +type InferGetters<T> = {  | 
 | 31 | +  [Key in keyof T]: T[Key] extends (state: any, getters: any) => infer R ? R : never  | 
22 | 32 | };  | 
23 | 33 | 
 
  | 
24 |  | -type InferGetters<T> = { [Key in keyof T]: T extends any ? ReturnType<T[Key]> : never };  | 
 | 34 | +type InferState<S> = S extends () => infer S ? S : S;  | 
25 | 35 | 
 
  | 
26 | 36 | export function Context<  | 
27 | 37 |   S extends (() => object) | object = {},  | 
28 | 38 |   M extends MutationTree<any> = {},  | 
29 | 39 |   A extends ActionTree<any, any> = {},  | 
30 | 40 |   G extends GetterTree<any, any> = {}  | 
31 | 41 | >() {  | 
32 |  | -  type State = S extends () => object ? ReturnType<S> : S;  | 
33 |  | - | 
34 |  | -  const InstanceType = (null as unknown) as Readonly<{  | 
35 |  | -    state: State;  | 
 | 42 | +  type InstanceType = Readonly<{  | 
 | 43 | +    state: InferState<S>;  | 
36 | 44 |     commit: InferMutations<M>;  | 
37 | 45 |     dispatch: InferActions<A>;  | 
38 | 46 |     getters: InferGetters<G>;  | 
39 | 47 |   }>;  | 
40 | 48 | 
 
  | 
41 | 49 |   return {  | 
42 |  | -    InstanceType,  | 
 | 50 | +    InstanceType: (undefined as unknown) as InstanceType,  | 
 | 51 | + | 
 | 52 | +    getGetters(getters: any): InstanceType['getters'] {  | 
 | 53 | +      return getters;  | 
 | 54 | +    },  | 
43 | 55 | 
 
  | 
44 |  | -    getInstance(store: Store<any> | ActionContext<any, any>, ns: string = ''): typeof InstanceType {  | 
 | 56 | +    getInstance(store: Store<any> | ActionContext<any, any>, ns: string = ''): InstanceType {  | 
45 | 57 |       const splitNs = ns ? ns.split('/').filter(val => !!val) : [];  | 
46 | 58 |       const fixedNs = splitNs.length ? splitNs.join('/') + '/' : '';  | 
47 | 59 | 
 
  | 
 | 
0 commit comments