index.d.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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, options?: SubscribeOptions): () => void;
  14. subscribeAction<P extends ActionPayload>(fn: SubscribeActionOptions<P, S>, options?: SubscribeOptions): () => 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 interface SubscribeOptions {
  56. prepend?: boolean
  57. }
  58. export type ActionSubscriber<P, S> = (action: P, state: S) => any;
  59. export type ActionErrorSubscriber<P, S> = (action: P, state: S, error: Error) => any;
  60. export interface ActionSubscribersObject<P, S> {
  61. before?: ActionSubscriber<P, S>;
  62. after?: ActionSubscriber<P, S>;
  63. error?: ActionErrorSubscriber<P, S>;
  64. }
  65. export type SubscribeActionOptions<P, S> = ActionSubscriber<P, S> | ActionSubscribersObject<P, S>;
  66. export interface DispatchOptions {
  67. root?: boolean;
  68. }
  69. export interface CommitOptions {
  70. silent?: boolean;
  71. root?: boolean;
  72. }
  73. export interface StoreOptions<S> {
  74. state?: S | (() => S);
  75. getters?: GetterTree<S, S>;
  76. actions?: ActionTree<S, S>;
  77. mutations?: MutationTree<S>;
  78. modules?: ModuleTree<S>;
  79. plugins?: Plugin<S>[];
  80. strict?: boolean;
  81. devtools?: boolean;
  82. }
  83. export type ActionHandler<S, R> = (this: Store<R>, injectee: ActionContext<S, R>, payload?: any) => any;
  84. export interface ActionObject<S, R> {
  85. root?: boolean;
  86. handler: ActionHandler<S, R>;
  87. }
  88. export type Getter<S, R> = (state: S, getters: any, rootState: R, rootGetters: any) => any;
  89. export type Action<S, R> = ActionHandler<S, R> | ActionObject<S, R>;
  90. export type Mutation<S> = (state: S, payload?: any) => any;
  91. export type Plugin<S> = (store: Store<S>) => any;
  92. export interface Module<S, R> {
  93. namespaced?: boolean;
  94. state?: S | (() => S);
  95. getters?: GetterTree<S, R>;
  96. actions?: ActionTree<S, R>;
  97. mutations?: MutationTree<S>;
  98. modules?: ModuleTree<R>;
  99. }
  100. export interface ModuleOptions {
  101. preserveState?: boolean;
  102. }
  103. export interface GetterTree<S, R> {
  104. [key: string]: Getter<S, R>;
  105. }
  106. export interface ActionTree<S, R> {
  107. [key: string]: Action<S, R>;
  108. }
  109. export interface MutationTree<S> {
  110. [key: string]: Mutation<S>;
  111. }
  112. export interface ModuleTree<R> {
  113. [key: string]: Module<any, R>;
  114. }
  115. declare const _default: {
  116. Store: typeof Store;
  117. install: typeof install;
  118. mapState: typeof mapState,
  119. mapMutations: typeof mapMutations,
  120. mapGetters: typeof mapGetters,
  121. mapActions: typeof mapActions,
  122. createNamespacedHelpers: typeof createNamespacedHelpers,
  123. };
  124. export default _default;