UNPKG

2.95 kBTypeScriptView Raw
1import { ComponentPublicInstance } from 'vue';
2import { Dispatch, Commit } from './index';
3
4type Computed = () => any;
5type InlineComputed<T extends Function> = T extends (...args: any[]) => infer R ? () => R : never
6type MutationMethod = (...args: any[]) => void;
7type ActionMethod = (...args: any[]) => Promise<any>;
8type InlineMethod<T extends (fn: any, ...args: any[]) => any> = T extends (fn: any, ...args: infer Args) => infer R ? (...args: Args) => R : never
9type CustomVue = ComponentPublicInstance & Record<string, any>;
10
11interface Mapper<R> {
12 <Key extends string>(map: Key[]): { [K in Key]: R };
13 <Map extends Record<string, string>>(map: Map): { [K in keyof Map]: R };
14}
15
16interface MapperWithNamespace<R> {
17 <Key extends string>(namespace: string, map: Key[]): { [K in Key]: R };
18 <Map extends Record<string, string>>(namespace: string, map: Map): { [K in keyof Map]: R };
19}
20
21interface MapperForState {
22 <S, Map extends Record<string, (this: CustomVue, state: S, getters: any) => any> = {}>(
23 map: Map
24 ): { [K in keyof Map]: InlineComputed<Map[K]> };
25}
26
27interface MapperForStateWithNamespace {
28 <S, Map extends Record<string, (this: CustomVue, state: S, getters: any) => any> = {}>(
29 namespace: string,
30 map: Map
31 ): { [K in keyof Map]: InlineComputed<Map[K]> };
32}
33
34interface MapperForAction {
35 <Map extends Record<string, (this: CustomVue, dispatch: Dispatch, ...args: any[]) => any>>(
36 map: Map
37 ): { [K in keyof Map]: InlineMethod<Map[K]> };
38}
39
40interface MapperForActionWithNamespace {
41 <Map extends Record<string, (this: CustomVue, dispatch: Dispatch, ...args: any[]) => any>>(
42 namespace: string,
43 map: Map
44 ): { [K in keyof Map]: InlineMethod<Map[K]> };
45}
46
47interface MapperForMutation {
48 <Map extends Record<string, (this: CustomVue, commit: Commit, ...args: any[]) => any>>(
49 map: Map
50 ): { [K in keyof Map]: InlineMethod<Map[K]> };
51}
52
53interface MapperForMutationWithNamespace {
54 <Map extends Record<string, (this: CustomVue, commit: Commit, ...args: any[]) => any>>(
55 namespace: string,
56 map: Map
57 ): { [K in keyof Map]: InlineMethod<Map[K]> };
58}
59
60
61interface NamespacedMappers {
62 mapState: Mapper<Computed> & MapperForState;
63 mapMutations: Mapper<MutationMethod> & MapperForMutation;
64 mapGetters: Mapper<Computed>;
65 mapActions: Mapper<ActionMethod> & MapperForAction;
66}
67
68export declare const mapState: Mapper<Computed>
69 & MapperWithNamespace<Computed>
70 & MapperForState
71 & MapperForStateWithNamespace;
72
73export declare const mapMutations: Mapper<MutationMethod>
74 & MapperWithNamespace<MutationMethod>
75 & MapperForMutation
76 & MapperForMutationWithNamespace;
77
78export declare const mapGetters: Mapper<Computed>
79 & MapperWithNamespace<Computed>;
80
81export declare const mapActions: Mapper<ActionMethod>
82 & MapperWithNamespace<ActionMethod>
83 & MapperForAction
84 & MapperForActionWithNamespace;
85
86export declare function createNamespacedHelpers(namespace: string): NamespacedMappers;