Skip to content

Commit d1b5c66

Browse files
sue71ktsn
authored andcommitted
fix(types): add type annotation for the context of actions (#1322)
* feat(types): add type annotation for the context of actions and mutations * fix(types): remove this annotation from mutations
1 parent 7eeeca0 commit d1b5c66

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

types/index.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export interface StoreOptions<S> {
8686
strict?: boolean;
8787
}
8888

89-
export type ActionHandler<S, R> = (injectee: ActionContext<S, R>, payload: any) => any;
89+
export type ActionHandler<S, R> = (this: Store<R>, injectee: ActionContext<S, R>, payload: any) => any;
9090
export interface ActionObject<S, R> {
9191
root?: boolean;
9292
handler: ActionHandler<S, R>;
@@ -129,5 +129,5 @@ export interface ModuleTree<R> {
129129
declare const _default: {
130130
Store: typeof Store;
131131
install: typeof install;
132-
}
132+
};
133133
export default _default;

types/test/index.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ 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", {});
@@ -83,6 +84,7 @@ namespace RootDefaultModule {
8384
},
8485
actions: {
8586
foo ({ state, getters, dispatch, commit }, payload) {
87+
this.state.value;
8688
state.value;
8789
getters.count;
8890
dispatch("bar", {});
@@ -107,7 +109,10 @@ namespace NestedModules {
107109
};
108110
d: {
109111
value: number;
110-
};
112+
},
113+
e: {
114+
value: number;
115+
}
111116
};
112117
}
113118

@@ -145,7 +150,17 @@ namespace NestedModules {
145150
b: {
146151
modules: {
147152
c: module,
148-
d: module
153+
d: module,
154+
e: {
155+
state: {
156+
value: 0
157+
},
158+
actions: {
159+
foo(context: ActionStore, payload) {
160+
this.state.a;
161+
}
162+
}
163+
}
149164
}
150165
}
151166
}

0 commit comments

Comments
 (0)