Pārlūkot izejas kodu

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
Sue 6 gadi atpakaļ
vecāks
revīzija
d1b5c66961
2 mainītis faili ar 19 papildinājumiem un 4 dzēšanām
  1. 2 2
      types/index.d.ts
  2. 17 2
      types/test/index.ts

+ 2 - 2
types/index.d.ts

@@ -86,7 +86,7 @@ export interface StoreOptions<S> {
   strict?: boolean;
 }
 
-export type ActionHandler<S, R> = (injectee: ActionContext<S, R>, payload: any) => any;
+export type ActionHandler<S, R> = (this: Store<R>, injectee: ActionContext<S, R>, payload: any) => any;
 export interface ActionObject<S, R> {
   root?: boolean;
   handler: ActionHandler<S, R>;
@@ -129,5 +129,5 @@ export interface ModuleTree<R> {
 declare const _default: {
   Store: typeof Store;
   install: typeof install;
-}
+};
 export default _default;

+ 17 - 2
types/test/index.ts

@@ -59,6 +59,7 @@ namespace RootModule {
     },
     actions: {
       foo ({ state, getters, dispatch, commit }, payload) {
+        this.state.value;
         state.value;
         getters.count;
         dispatch("bar", {});
@@ -83,6 +84,7 @@ namespace RootDefaultModule {
     },
     actions: {
       foo ({ state, getters, dispatch, commit }, payload) {
+        this.state.value;
         state.value;
         getters.count;
         dispatch("bar", {});
@@ -107,7 +109,10 @@ namespace NestedModules {
       };
       d: {
         value: number;
-      };
+      },
+      e: {
+        value: number;
+      }
     };
   }
 
@@ -145,7 +150,17 @@ namespace NestedModules {
       b: {
         modules: {
           c: module,
-          d: module
+          d: module,
+          e: {
+            state: {
+              value: 0
+            },
+            actions: {
+              foo(context: ActionStore, payload) {
+                this.state.a;
+              }
+            }
+          }
         }
       }
     }