index.d.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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): () => void;
  15. subscribeAction<P extends ActionPayload>(fn: SubscribeActionOptions<P, S>): () => 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 type ActionSubscriber<P, S> = (action: P, state: S) => any;
  57. export interface ActionSubscribersObject<P, S> {
  58. before?: ActionSubscriber<P, S>;
  59. after?: ActionSubscriber<P, S>;
  60. }
  61. export type SubscribeActionOptions<P, S> = ActionSubscriber<P, S> | ActionSubscribersObject<P, S>;
  62. export interface DispatchOptions {
  63. root?: boolean;
  64. }
  65. export interface CommitOptions {
  66. silent?: boolean;
  67. root?: boolean;
  68. }
  69. export interface StoreOptions<S> {
  70. state?: S | (() => S);
  71. getters?: GetterTree<S, S>;
  72. actions?: ActionTree<S, S>;
  73. mutations?: MutationTree<S>;
  74. modules?: ModuleTree<S>;
  75. plugins?: Plugin<S>[];
  76. strict?: boolean;
  77. }
  78. export type ActionHandler<S, R> = (this: Store<R>, injectee: ActionContext<S, R>, payload?: any) => any;
  79. export interface ActionObject<S, R> {
  80. root?: boolean;
  81. handler: ActionHandler<S, R>;
  82. }
  83. export type Getter<S, R> = (state: S, getters: any, rootState: R, rootGetters: any) => any;
  84. export type Action<S, R> = ActionHandler<S, R> | ActionObject<S, R>;
  85. export type Mutation<S> = (state: S, payload?: any) => any;
  86. export type Plugin<S> = (store: Store<S>) => any;
  87. export interface Module<S, R> {
  88. namespaced?: boolean;
  89. state?: S | (() => S);
  90. getters?: GetterTree<S, R>;
  91. actions?: ActionTree<S, R>;
  92. mutations?: MutationTree<S>;
  93. modules?: ModuleTree<R>;
  94. }
  95. export interface ModuleOptions {
  96. preserveState?: boolean;
  97. }
  98. export interface GetterTree<S, R> {
  99. [key: string]: Getter<S, R>;
  100. }
  101. export interface ActionTree<S, R> {
  102. [key: string]: Action<S, R>;
  103. }
  104. export interface MutationTree<S> {
  105. [key: string]: Mutation<S>;
  106. }
  107. export interface ModuleTree<R> {
  108. [key: string]: Module<any, R>;
  109. }
  110. declare const _default: {
  111. Store: typeof Store;
  112. mapState: typeof mapState,
  113. mapMutations: typeof mapMutations,
  114. mapGetters: typeof mapGetters,
  115. mapActions: typeof mapActions,
  116. createNamespacedHelpers: typeof createNamespacedHelpers,
  117. };
  118. export default _default;