index.d.ts 4.2 KB

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