Skip to content

Commit 2c29024

Browse files
committed
feat(types): add type annotation for the context of actions and mutations
1 parent 7eeeca0 commit 2c29024

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

Diff for: types/index.d.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export declare class Store<S> {
2828

2929
hotUpdate(options: {
3030
actions?: ActionTree<S, S>;
31-
mutations?: MutationTree<S>;
31+
mutations?: MutationTree<S, S>;
3232
getters?: GetterTree<S, S>;
3333
modules?: ModuleTree<S>;
3434
}): void;
@@ -48,6 +48,7 @@ export interface Commit {
4848

4949
export interface ActionContext<S, R> {
5050
dispatch: Dispatch;
51+
5152
commit: Commit;
5253
state: S;
5354
getters: any;
@@ -80,29 +81,29 @@ export interface StoreOptions<S> {
8081
state?: S;
8182
getters?: GetterTree<S, S>;
8283
actions?: ActionTree<S, S>;
83-
mutations?: MutationTree<S>;
84+
mutations?: MutationTree<S, S>;
8485
modules?: ModuleTree<S>;
8586
plugins?: Plugin<S>[];
8687
strict?: boolean;
8788
}
8889

89-
export type ActionHandler<S, R> = (injectee: ActionContext<S, R>, payload: any) => any;
90+
export type ActionHandler<S, R> = (this: Store<R>, injectee: ActionContext<S, R>, payload: any) => any;
9091
export interface ActionObject<S, R> {
9192
root?: boolean;
9293
handler: ActionHandler<S, R>;
9394
}
9495

9596
export type Getter<S, R> = (state: S, getters: any, rootState: R, rootGetters: any) => any;
9697
export type Action<S, R> = ActionHandler<S, R> | ActionObject<S, R>;
97-
export type Mutation<S> = (state: S, payload: any) => any;
98+
export type Mutation<S, R> = (this: Store<R>, state: S, payload: any) => any;
9899
export type Plugin<S> = (store: Store<S>) => any;
99100

100101
export interface Module<S, R> {
101102
namespaced?: boolean;
102103
state?: S | (() => S);
103104
getters?: GetterTree<S, R>;
104105
actions?: ActionTree<S, R>;
105-
mutations?: MutationTree<S>;
106+
mutations?: MutationTree<S, R>;
106107
modules?: ModuleTree<R>;
107108
}
108109

@@ -118,8 +119,8 @@ export interface ActionTree<S, R> {
118119
[key: string]: Action<S, R>;
119120
}
120121

121-
export interface MutationTree<S> {
122-
[key: string]: Mutation<S>;
122+
export interface MutationTree<S, R> {
123+
[key: string]: Mutation<S, R>;
123124
}
124125

125126
export interface ModuleTree<R> {
@@ -129,5 +130,5 @@ export interface ModuleTree<R> {
129130
declare const _default: {
130131
Store: typeof Store;
131132
install: typeof install;
132-
}
133+
};
133134
export default _default;

Diff for: types/test/index.ts

+28-4
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,17 @@ namespace RootModule {
5959
},
6060
actions: {
6161
foo ({ state, getters, dispatch, commit }, payload) {
62+
this.state.value;
6263
state.value;
6364
getters.count;
6465
dispatch("bar", {});
6566
commit("bar", {});
6667
}
6768
},
6869
mutations: {
69-
bar (state, payload) {}
70+
bar (state, payload) {
71+
this.state.value;
72+
}
7073
},
7174
strict: true
7275
});
@@ -83,14 +86,17 @@ namespace RootDefaultModule {
8386
},
8487
actions: {
8588
foo ({ state, getters, dispatch, commit }, payload) {
89+
this.state.value;
8690
state.value;
8791
getters.count;
8892
dispatch("bar", {});
8993
commit("bar", {});
9094
}
9195
},
9296
mutations: {
93-
bar (state, payload) {}
97+
bar (state, payload) {
98+
this.state.value;
99+
}
94100
},
95101
strict: true
96102
});
@@ -107,7 +113,10 @@ namespace NestedModules {
107113
};
108114
d: {
109115
value: number;
110-
};
116+
},
117+
e: {
118+
value: number;
119+
}
111120
};
112121
}
113122

@@ -145,7 +154,22 @@ namespace NestedModules {
145154
b: {
146155
modules: {
147156
c: module,
148-
d: module
157+
d: module,
158+
e: {
159+
state: {
160+
value: 0
161+
},
162+
actions: {
163+
foo(context: ActionStore, payload) {
164+
this.state.a;
165+
}
166+
},
167+
mutations: {
168+
bar(state, payload) {
169+
this.state.b.e;
170+
}
171+
}
172+
}
149173
}
150174
}
151175
}

0 commit comments

Comments
 (0)