helpers.d.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { ComponentPublicInstance } from 'vue';
  2. import { Dispatch, Commit } from './index';
  3. type Computed = () => any;
  4. type InlineComputed<T extends Function> = T extends (...args: any[]) => infer R ? () => R : never
  5. type MutationMethod = (...args: any[]) => void;
  6. type ActionMethod = (...args: any[]) => Promise<any>;
  7. type InlineMethod<T extends (fn: any, ...args: any[]) => any> = T extends (fn: any, ...args: infer Args) => infer R ? (...args: Args) => R : never
  8. type CustomVue = ComponentPublicInstance & Record<string, any>;
  9. interface Mapper<R> {
  10. <Key extends string>(map: Key[]): { [K in Key]: R };
  11. <Map extends Record<string, string>>(map: Map): { [K in keyof Map]: R };
  12. }
  13. interface MapperWithNamespace<R> {
  14. <Key extends string>(namespace: string, map: Key[]): { [K in Key]: R };
  15. <Map extends Record<string, string>>(namespace: string, map: Map): { [K in keyof Map]: R };
  16. }
  17. interface MapperForState {
  18. <S, Map extends Record<string, (this: CustomVue, state: S, getters: any) => any> = {}>(
  19. map: Map
  20. ): { [K in keyof Map]: InlineComputed<Map[K]> };
  21. }
  22. interface MapperForStateWithNamespace {
  23. <S, Map extends Record<string, (this: CustomVue, state: S, getters: any) => any> = {}>(
  24. namespace: string,
  25. map: Map
  26. ): { [K in keyof Map]: InlineComputed<Map[K]> };
  27. }
  28. interface MapperForAction {
  29. <Map extends Record<string, (this: CustomVue, dispatch: Dispatch, ...args: any[]) => any>>(
  30. map: Map
  31. ): { [K in keyof Map]: InlineMethod<Map[K]> };
  32. }
  33. interface MapperForActionWithNamespace {
  34. <Map extends Record<string, (this: CustomVue, dispatch: Dispatch, ...args: any[]) => any>>(
  35. namespace: string,
  36. map: Map
  37. ): { [K in keyof Map]: InlineMethod<Map[K]> };
  38. }
  39. interface MapperForMutation {
  40. <Map extends Record<string, (this: CustomVue, commit: Commit, ...args: any[]) => any>>(
  41. map: Map
  42. ): { [K in keyof Map]: InlineMethod<Map[K]> };
  43. }
  44. interface MapperForMutationWithNamespace {
  45. <Map extends Record<string, (this: CustomVue, commit: Commit, ...args: any[]) => any>>(
  46. namespace: string,
  47. map: Map
  48. ): { [K in keyof Map]: InlineMethod<Map[K]> };
  49. }
  50. interface NamespacedMappers {
  51. mapState: Mapper<Computed> & MapperForState;
  52. mapMutations: Mapper<MutationMethod> & MapperForMutation;
  53. mapGetters: Mapper<Computed>;
  54. mapActions: Mapper<ActionMethod> & MapperForAction;
  55. }
  56. export declare const mapState: Mapper<Computed>
  57. & MapperWithNamespace<Computed>
  58. & MapperForState
  59. & MapperForStateWithNamespace;
  60. export declare const mapMutations: Mapper<MutationMethod>
  61. & MapperWithNamespace<MutationMethod>
  62. & MapperForMutation
  63. & MapperForMutationWithNamespace;
  64. export declare const mapGetters: Mapper<Computed>
  65. & MapperWithNamespace<Computed>;
  66. export declare const mapActions: Mapper<ActionMethod>
  67. & MapperWithNamespace<ActionMethod>
  68. & MapperForAction
  69. & MapperForActionWithNamespace;
  70. export declare function createNamespacedHelpers(namespace: string): NamespacedMappers;