index.d.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import _Vue = require("vue");
  2. import { WatchOptions } from "vue";
  3. // augment typings of Vue.js
  4. import "./vue";
  5. export * from "./helpers";
  6. export declare class Store<S> {
  7. constructor(options: StoreOptions<S>);
  8. readonly state: S;
  9. readonly getters: any;
  10. replaceState(state: S): void;
  11. dispatch: Dispatch;
  12. commit: Commit;
  13. subscribe<P extends MutationPayload>(fn: (mutation: P, state: S) => any): () => void;
  14. watch<T>(getter: (state: S) => T, cb: (value: T, oldValue: T) => void, options?: WatchOptions): () => void;
  15. registerModule<T>(path: string, module: Module<T, S>, options?: ModuleOptions): void;
  16. registerModule<T>(path: string[], module: Module<T, S>, options?: ModuleOptions): void;
  17. unregisterModule(path: string): void;
  18. unregisterModule(path: string[]): void;
  19. hotUpdate(options: {
  20. actions?: ActionTree<S, S>;
  21. mutations?: MutationTree<S>;
  22. getters?: GetterTree<S, S>;
  23. modules?: ModuleTree<S>;
  24. }): void;
  25. }
  26. export declare function install(Vue: typeof _Vue): void;
  27. export interface Dispatch {
  28. (type: string, payload?: any, options?: DispatchOptions): Promise<any>;
  29. <P extends Payload>(payloadWithType: P, options?: DispatchOptions): Promise<any>;
  30. }
  31. export interface Commit {
  32. (type: string, payload?: any, options?: CommitOptions): void;
  33. <P extends Payload>(payloadWithType: P, options?: CommitOptions): void;
  34. }
  35. export interface ActionContext<S, R> {
  36. dispatch: Dispatch;
  37. commit: Commit;
  38. state: S;
  39. getters: any;
  40. rootState: R;
  41. rootGetters: any;
  42. }
  43. export interface Payload {
  44. type: string;
  45. }
  46. export interface MutationPayload extends Payload {
  47. payload: any;
  48. }
  49. export interface DispatchOptions {
  50. root?: boolean;
  51. }
  52. export interface CommitOptions {
  53. silent?: boolean;
  54. root?: boolean;
  55. }
  56. export interface StoreOptions<S> {
  57. state?: S;
  58. getters?: GetterTree<S, S>;
  59. actions?: ActionTree<S, S>;
  60. mutations?: MutationTree<S>;
  61. modules?: ModuleTree<S>;
  62. plugins?: Plugin<S>[];
  63. strict?: boolean;
  64. }
  65. type ActionHandler<S, R> = (injectee: ActionContext<S, R>, payload: any) => any;
  66. interface ActionObject<S, R> {
  67. root?: boolean;
  68. handler: ActionHandler<S, R>;
  69. }
  70. export type Getter<S, R> = (state: S, getters: any, rootState: R, rootGetters: any) => any;
  71. export type Action<S, R> = ActionHandler<S, R> | ActionObject<S, R>;
  72. export type Mutation<S> = (state: S, payload: any) => any;
  73. export type Plugin<S> = (store: Store<S>) => any;
  74. export interface Module<S, R> {
  75. namespaced?: boolean;
  76. state?: S | (() => S);
  77. getters?: GetterTree<S, R>;
  78. actions?: ActionTree<S, R>;
  79. mutations?: MutationTree<S>;
  80. modules?: ModuleTree<R>;
  81. }
  82. export interface ModuleOptions{
  83. preserveState?: boolean
  84. }
  85. export interface GetterTree<S, R> {
  86. [key: string]: Getter<S, R>;
  87. }
  88. export interface ActionTree<S, R> {
  89. [key: string]: Action<S, R>;
  90. }
  91. export interface MutationTree<S> {
  92. [key: string]: Mutation<S>;
  93. }
  94. export interface ModuleTree<R> {
  95. [key: string]: Module<any, R>;
  96. }