UNPKG

3.84 kBTypeScriptView Raw
1import _Vue, { WatchOptions } from "vue";
2
3// augment typings of Vue.js
4import "./vue";
5
6import { mapState, mapMutations, mapGetters, mapActions, createNamespacedHelpers } from "./helpers";
7
8export * from "./helpers";
9
10export declare class Store<S> {
11 constructor(options: StoreOptions<S>);
12
13 readonly state: S;
14 readonly getters: any;
15
16 replaceState(state: S): void;
17
18 dispatch: Dispatch;
19 commit: Commit;
20
21 subscribe<P extends MutationPayload>(fn: (mutation: P, state: S) => any): () => void;
22 subscribeAction<P extends ActionPayload>(fn: SubscribeActionOptions<P, S>): () => void;
23 watch<T>(getter: (state: S, getters: any) => T, cb: (value: T, oldValue: T) => void, options?: WatchOptions): () => void;
24
25 registerModule<T>(path: string, module: Module<T, S>, options?: ModuleOptions): void;
26 registerModule<T>(path: string[], module: Module<T, S>, options?: ModuleOptions): void;
27
28 unregisterModule(path: string): void;
29 unregisterModule(path: string[]): void;
30
31 hotUpdate(options: {
32 actions?: ActionTree<S, S>;
33 mutations?: MutationTree<S>;
34 getters?: GetterTree<S, S>;
35 modules?: ModuleTree<S>;
36 }): void;
37}
38
39export declare function install(Vue: typeof _Vue): void;
40
41export interface Dispatch {
42 (type: string, payload?: any, options?: DispatchOptions): Promise<any>;
43 <P extends Payload>(payloadWithType: P, options?: DispatchOptions): Promise<any>;
44}
45
46export interface Commit {
47 (type: string, payload?: any, options?: CommitOptions): void;
48 <P extends Payload>(payloadWithType: P, options?: CommitOptions): void;
49}
50
51export interface ActionContext<S, R> {
52 dispatch: Dispatch;
53 commit: Commit;
54 state: S;
55 getters: any;
56 rootState: R;
57 rootGetters: any;
58}
59
60export interface Payload {
61 type: string;
62}
63
64export interface MutationPayload extends Payload {
65 payload: any;
66}
67
68export interface ActionPayload extends Payload {
69 payload: any;
70}
71
72export type ActionSubscriber<P, S> = (action: P, state: S) => any;
73
74export interface ActionSubscribersObject<P, S> {
75 before?: ActionSubscriber<P, S>;
76 after?: ActionSubscriber<P, S>;
77}
78
79export type SubscribeActionOptions<P, S> = ActionSubscriber<P, S> | ActionSubscribersObject<P, S>;
80
81export interface DispatchOptions {
82 root?: boolean;
83}
84
85export interface CommitOptions {
86 silent?: boolean;
87 root?: boolean;
88}
89
90export interface StoreOptions<S> {
91 state?: S | (() => S);
92 getters?: GetterTree<S, S>;
93 actions?: ActionTree<S, S>;
94 mutations?: MutationTree<S>;
95 modules?: ModuleTree<S>;
96 plugins?: Plugin<S>[];
97 strict?: boolean;
98}
99
100export type ActionHandler<S, R> = (this: Store<R>, injectee: ActionContext<S, R>, payload?: any) => any;
101export interface ActionObject<S, R> {
102 root?: boolean;
103 handler: ActionHandler<S, R>;
104}
105
106export type Getter<S, R> = (state: S, getters: any, rootState: R, rootGetters: any) => any;
107export type Action<S, R> = ActionHandler<S, R> | ActionObject<S, R>;
108export type Mutation<S> = (state: S, payload?: any) => any;
109export type Plugin<S> = (store: Store<S>) => any;
110
111export interface Module<S, R> {
112 namespaced?: boolean;
113 state?: S | (() => S);
114 getters?: GetterTree<S, R>;
115 actions?: ActionTree<S, R>;
116 mutations?: MutationTree<S>;
117 modules?: ModuleTree<R>;
118}
119
120export interface ModuleOptions {
121 preserveState?: boolean;
122}
123
124export interface GetterTree<S, R> {
125 [key: string]: Getter<S, R>;
126}
127
128export interface ActionTree<S, R> {
129 [key: string]: Action<S, R>;
130}
131
132export interface MutationTree<S> {
133 [key: string]: Mutation<S>;
134}
135
136export interface ModuleTree<R> {
137 [key: string]: Module<any, R>;
138}
139
140declare const _default: {
141 Store: typeof Store;
142 install: typeof install;
143 mapState: typeof mapState,
144 mapMutations: typeof mapMutations,
145 mapGetters: typeof mapGetters,
146 mapActions: typeof mapActions,
147 createNamespacedHelpers: typeof createNamespacedHelpers,
148};
149export default _default;
150
\No newline at end of file