index.d.ts 3.5 KB

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