index.d.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import _Vue, { WatchOptions } from "vue";
  2. // augment typings of Vue.js
  3. import "./vue";
  4. import { mapState, mapMutations, mapGetters, mapActions, createNamespacedHelpers } from "./helpers";
  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. subscribeAction<P extends ActionPayload>(fn: SubscribeActionOptions<P, S>): () => void;
  15. watch<T>(getter: (state: S, getters: any) => T, cb: (value: T, oldValue: T) => void, options?: WatchOptions): () => void;
  16. registerModule<T>(path: string, module: Module<T, S>, options?: ModuleOptions): void;
  17. registerModule<T>(path: string[], module: Module<T, S>, options?: ModuleOptions): void;
  18. unregisterModule(path: string): void;
  19. unregisterModule(path: string[]): void;
  20. hasModule(path: string): boolean;
  21. hasModule(path: string[]): boolean;
  22. hotUpdate(options: {
  23. actions?: ActionTree<S, S>;
  24. mutations?: MutationTree<S>;
  25. getters?: GetterTree<S, S>;
  26. modules?: ModuleTree<S>;
  27. }): void;
  28. }
  29. export declare function install(Vue: typeof _Vue): void;
  30. export interface Dispatch {
  31. (type: string, payload?: any, options?: DispatchOptions): Promise<any>;
  32. <P extends Payload>(payloadWithType: P, options?: DispatchOptions): Promise<any>;
  33. }
  34. export interface Commit {
  35. (type: string, payload?: any, options?: CommitOptions): void;
  36. <P extends Payload>(payloadWithType: P, options?: CommitOptions): void;
  37. }
  38. export interface ActionContext<S, R> {
  39. dispatch: Dispatch;
  40. commit: Commit;
  41. state: S;
  42. getters: any;
  43. rootState: R;
  44. rootGetters: any;
  45. }
  46. export interface Payload {
  47. type: string;
  48. }
  49. export interface MutationPayload extends Payload {
  50. payload: any;
  51. }
  52. export interface ActionPayload extends Payload {
  53. payload: any;
  54. }
  55. export type ActionSubscriber<P, S> = (action: P, state: S) => any;
  56. export interface ActionSubscribersObject<P, S> {
  57. before?: ActionSubscriber<P, S>;
  58. after?: ActionSubscriber<P, S>;
  59. }
  60. export type SubscribeActionOptions<P, S> = ActionSubscriber<P, S> | ActionSubscribersObject<P, S>;
  61. export interface DispatchOptions {
  62. root?: boolean;
  63. }
  64. export interface CommitOptions {
  65. silent?: boolean;
  66. root?: boolean;
  67. }
  68. export interface StoreOptions<S> {
  69. state?: S | (() => S);
  70. getters?: GetterTree<S, S>;
  71. actions?: ActionTree<S, S>;
  72. mutations?: MutationTree<S>;
  73. modules?: ModuleTree<S>;
  74. plugins?: Plugin<S>[];
  75. strict?: boolean;
  76. }
  77. export type ActionHandler<S, R> = (this: Store<R>, injectee: ActionContext<S, R>, payload?: any) => any;
  78. export interface ActionObject<S, R> {
  79. root?: boolean;
  80. handler: ActionHandler<S, R>;
  81. }
  82. export type Getter<S, R> = (state: S, getters: any, rootState: R, rootGetters: any) => any;
  83. export type Action<S, R> = ActionHandler<S, R> | ActionObject<S, R>;
  84. export type Mutation<S> = (state: S, payload?: any) => any;
  85. export type Plugin<S> = (store: Store<S>) => any;
  86. export interface Module<S, R> {
  87. namespaced?: boolean;
  88. state?: S | (() => S);
  89. getters?: GetterTree<S, R>;
  90. actions?: ActionTree<S, R>;
  91. mutations?: MutationTree<S>;
  92. modules?: ModuleTree<R>;
  93. }
  94. export interface ModuleOptions {
  95. preserveState?: boolean;
  96. }
  97. export interface GetterTree<S, R> {
  98. [key: string]: Getter<S, R>;
  99. }
  100. export interface ActionTree<S, R> {
  101. [key: string]: Action<S, R>;
  102. }
  103. export interface MutationTree<S> {
  104. [key: string]: Mutation<S>;
  105. }
  106. export interface ModuleTree<R> {
  107. [key: string]: Module<any, R>;
  108. }
  109. declare const _default: {
  110. Store: typeof Store;
  111. install: typeof install;
  112. mapState: typeof mapState,
  113. mapMutations: typeof mapMutations,
  114. mapGetters: typeof mapGetters,
  115. mapActions: typeof mapActions,
  116. createNamespacedHelpers: typeof createNamespacedHelpers,
  117. };
  118. export default _default;