Skip to content

Commit

Permalink
feat(types): add type annotation for the context of actions and mutat…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
sue71 committed Jul 18, 2018
1 parent 7eeeca0 commit 2c29024
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
17 changes: 9 additions & 8 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export declare class Store<S> {

hotUpdate(options: {
actions?: ActionTree<S, S>;
mutations?: MutationTree<S>;
mutations?: MutationTree<S, S>;
getters?: GetterTree<S, S>;
modules?: ModuleTree<S>;
}): void;
Expand All @@ -48,6 +48,7 @@ export interface Commit {

export interface ActionContext<S, R> {
dispatch: Dispatch;

commit: Commit;
state: S;
getters: any;
Expand Down Expand Up @@ -80,29 +81,29 @@ export interface StoreOptions<S> {
state?: S;
getters?: GetterTree<S, S>;
actions?: ActionTree<S, S>;
mutations?: MutationTree<S>;
mutations?: MutationTree<S, S>;
modules?: ModuleTree<S>;
plugins?: Plugin<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>;
}

export type Getter<S, R> = (state: S, getters: any, rootState: R, rootGetters: any) => any;
export type Action<S, R> = ActionHandler<S, R> | ActionObject<S, R>;
export type Mutation<S> = (state: S, payload: any) => any;
export type Mutation<S, R> = (this: Store<R>, state: S, payload: any) => any;
export type Plugin<S> = (store: Store<S>) => any;

export interface Module<S, R> {
namespaced?: boolean;
state?: S | (() => S);
getters?: GetterTree<S, R>;
actions?: ActionTree<S, R>;
mutations?: MutationTree<S>;
mutations?: MutationTree<S, R>;
modules?: ModuleTree<R>;
}

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

export interface MutationTree<S> {
[key: string]: Mutation<S>;
export interface MutationTree<S, R> {
[key: string]: Mutation<S, R>;
}

export interface ModuleTree<R> {
Expand All @@ -129,5 +130,5 @@ export interface ModuleTree<R> {
declare const _default: {
Store: typeof Store;
install: typeof install;
}
};
export default _default;
32 changes: 28 additions & 4 deletions types/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,17 @@ namespace RootModule {
},
actions: {
foo ({ state, getters, dispatch, commit }, payload) {
this.state.value;
state.value;
getters.count;
dispatch("bar", {});
commit("bar", {});
}
},
mutations: {
bar (state, payload) {}
bar (state, payload) {
this.state.value;
}
},
strict: true
});
Expand All @@ -83,14 +86,17 @@ namespace RootDefaultModule {
},
actions: {
foo ({ state, getters, dispatch, commit }, payload) {
this.state.value;
state.value;
getters.count;
dispatch("bar", {});
commit("bar", {});
}
},
mutations: {
bar (state, payload) {}
bar (state, payload) {
this.state.value;
}
},
strict: true
});
Expand All @@ -107,7 +113,10 @@ namespace NestedModules {
};
d: {
value: number;
};
},
e: {
value: number;
}
};
}

Expand Down Expand Up @@ -145,7 +154,22 @@ namespace NestedModules {
b: {
modules: {
c: module,
d: module
d: module,
e: {
state: {
value: 0
},
actions: {
foo(context: ActionStore, payload) {
this.state.a;
}
},
mutations: {
bar(state, payload) {
this.state.b.e;
}
}
}
}
}
}
Expand Down

0 comments on commit 2c29024

Please sign in to comment.