index.d.ts 4.0 KB

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