From d1b5c66961ab53e0172cbc706ff616227bcb5c77 Mon Sep 17 00:00:00 2001 From: Sue Date: Thu, 19 Jul 2018 00:11:07 +0900 Subject: [PATCH] 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 --- types/index.d.ts | 4 ++-- types/test/index.ts | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index 585b7d6b0..4c4e43e64 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -86,7 +86,7 @@ export interface StoreOptions { strict?: boolean; } -export type ActionHandler = (injectee: ActionContext, payload: any) => any; +export type ActionHandler = (this: Store, injectee: ActionContext, payload: any) => any; export interface ActionObject { root?: boolean; handler: ActionHandler; @@ -129,5 +129,5 @@ export interface ModuleTree { declare const _default: { Store: typeof Store; install: typeof install; -} +}; export default _default; diff --git a/types/test/index.ts b/types/test/index.ts index 0253e5df8..ca03458d9 100644 --- a/types/test/index.ts +++ b/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; + } + } + } } } }