index.d.ts 4.0 KB

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