index.d.ts 3.7 KB

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