1- import { computed } from 'vue-function-api' ;
2- import { mapState , mapGetters , mapMutations , mapActions } from 'vuex' ;
1+ import { computed , Wrapper } from 'vue-function-api' ;
2+ import {
3+ useState ,
4+ useGetters ,
5+ mapState ,
6+ mapGetters ,
7+ mapMutations ,
8+ mapActions ,
9+ } from 'vuex' ;
310import { getRuntimeVM } from './runtime' ;
411
512export enum Helper {
@@ -9,13 +16,13 @@ export enum Helper {
916 Actions ,
1017}
1118
12- function handleComputed ( mappedFn : Function ) {
19+ function handleComputed ( mappedFn : Function ) : Wrapper < any > {
1320 // TypeError: Cannot read property '_modulesNamespaceMap' of undefined
1421 // You must get `runtimeVM` in real time in the calculation properties.
1522 return computed ( ( ) => mappedFn . call ( getRuntimeVM ( ) ) ) ;
1623}
1724
18- function handleMethods ( mappedFn : Function ) {
25+ function handleMethods < T > ( mappedFn : Function ) : T {
1926 return mappedFn . bind ( getRuntimeVM ( ) ) ;
2027}
2128
@@ -26,17 +33,23 @@ const helpers = {
2633 [ Helper . Actions ] : { fn : mapActions , handler : handleMethods } ,
2734} ;
2835
29- export default function createVuexHelper ( h : Helper ) {
36+ export type Helpers =
37+ | typeof useState
38+ | typeof useGetters
39+ | typeof mapMutations
40+ | typeof mapActions ;
41+
42+ export default function createVuexHelper < T extends Helpers > ( h : Helper ) {
3043 const helper = helpers [ h ] ;
3144
3245 return ( ( ...args ) => {
3346 // @ts -ignore
34- const mapper = helper . fn ( ...args ) ;
47+ const mapper = ( helper . fn as T ) ( ...args ) ;
3548 const dictionary = { } ;
3649 Object . keys ( mapper ) . forEach ( ( key ) => {
37- dictionary [ key ] = helper . handler ( mapper [ key ] ) ;
50+ dictionary [ key ] = helper . handler < typeof mapper > ( mapper [ key ] ) ;
3851 } ) ;
3952
4053 return dictionary ;
41- } ) as typeof helper . fn ;
54+ } ) as T ;
4255}
0 commit comments