index.d.ts 4.4 KB

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