index.d.ts 4.3 KB

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