index.d.ts 4.2 KB

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