Skip to content

Instantly share code, notes, and snippets.

@sue71
Created March 29, 2018 01:00
Show Gist options
  • Save sue71/6239026e528593f637826cdbadaddc79 to your computer and use it in GitHub Desktop.
Save sue71/6239026e528593f637826cdbadaddc79 to your computer and use it in GitHub Desktop.
vuex type definition
import {
WatchOptions,
DispatchOptions,
CommitOptions,
MutationPayload,
} from 'vuex';
export interface ActionConvertible {
[key: string]: {
payload: string;
result: string;
};
}
export interface StoreOptions<S, M, G, A extends ActionsConvertible> {
state?: S;
getters?: GetterTree<S, S, G>;
mutations?: MutationTree<S, M>;
actions?: ActionTree<S, S, M, G, A>;
modules?: ModuleTree<any, S, M, G, A>;
plugins?: Plugin<S, M, G, A>[];
strict?: boolean;
}
export type Plugin<S, M, G, A extends ActionsConvertible> = (
store: Store<S, M, G, A>
) => any;
export interface Module<S, R, M, G, A extends ActionsConvertible> {
namespaced?: boolean;
state?: S | (() => S);
getters?: GetterTree<S, M, G>;
actions?: ActionTree<S, R, M, G, A>;
mutations?: MutationTree<S, M>;
modules?: ModuleTree<S, R, M, G, A>;
}
export type ModuleTree<S, R, M, G, A extends ActionsConvertible> = {
[K in keyof R]: Module<S, R, M, G, A>
}
export interface ModuleOptions {
preserveState?: boolean;
}
export class Store<S, M, G, A extends ActionsConvertible> {
constructor(_: StoreOptions<S, M, G, A>) {}
readonly state: S;
readonly getters: G;
replaceState: (state: S) => void;
commit: Commit<M>;
dispatch: Dispatch<A>;
subscribe: <P extends MutationPayload>(
fn: (mutation: P, state: S) => any
) => () => void;
watch: <T>(
getter: (state: S) => T,
cb: (value: T, oldValue: T) => void,
options?: WatchOptions
) => () => void;
registerModule: <S, M, G, A extends ActionConvertible>(
path: string,
module: Module<S, S, M, G, A>,
options?: ModuleOptions
) => void;
unregisterModule: (path: string | string[]) => void;
hotUpdate: <S, M, G, A extends ActionConvertible>(options: {
actions?: ActionTree<S, S, M, G, A>;
mutations?: MutationTree<S, M>;
getters?: GetterTree<S, M, G>;
modules?: ModuleTree<S, S, M, G, A>;
}) => void;
}
export type ActionsConvertible = {
[key: string]: {
payload: any;
result: any;
};
};
interface Commit<M> {
<P extends keyof M>(
payloadWithType: { type: P; payload?: M[P] },
options?: CommitOptions
): void;
}
interface Dispatch<A extends ActionsConvertible> {
<P extends keyof A>(
payloadWithType: { type: P; payload?: A[P]['payload'] },
options?: DispatchOptions
): Promise<A[P]['result']>;
<P extends keyof A>(
payloadWithType: { type: P; payload?: A[P]['payload'] },
options?: DispatchOptions
): void;
}
export interface ActionContext<S, R, M, G, A extends ActionsConvertible> {
dispatch: Dispatch<A>;
commit: Commit<M>;
state: S;
getters: any;
rootS: R;
rootG: G;
}
export type ActionTree<S, R, M, G, A extends ActionsConvertible> = {
[P in keyof A]: (
context: ActionContext<S, R, M, G, A>,
payload: A[P]['payload']
) => Promise<A[P]['result']> | void
};
export type MutationTree<S, M> = {
[P in keyof M]: (state: S, payload: M[P]) => void
};
export type Getter<S, R, G, RG> = (
state: S,
getters: G,
rootState: R,
rootGetters: RG
) => any;
export type GetterTree<S, R, G> = { [K in keyof G]: Getter<S, R, G[K], G> };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment