11import type { Location , Action , History } from 'history' ;
2- import type { AnyAction as ReduxAction } from 'redux' ;
32
43export const CALL_HISTORY_METHOD = '@@router/CALL_HISTORY_METHOD' ;
54export const LOCATION_CHANGE = '@@router/LOCATION_CHANGE' ;
@@ -19,22 +18,43 @@ export const locationChangeAction = (location: Location, action: Action) => ({
1918 payload : { location, action } as { location : Location ; action : Action } ,
2019} ) ;
2120
21+ export interface LocationActionPayload < A = unknown [ ] > {
22+ method : string ;
23+ args ?: A ;
24+ }
25+
26+ export interface CallHistoryMethodAction < A = unknown [ ] > {
27+ type : typeof CALL_HISTORY_METHOD ;
28+ payload : LocationActionPayload < A > ;
29+ }
30+
2231function updateLocation < T extends HistoryMethods > ( method : T ) {
23- // @ts -ignore
24- return ( ...args : Parameters < History [ T ] > ) : ReduxAction => ( {
32+ // @ts -ignore //support history 5.x back/forward
33+ return ( ...args : Parameters < History [ T ] > ) : CallHistoryMethodAction < Parameters < History [ T ] > > => ( {
2534 type : CALL_HISTORY_METHOD as typeof CALL_HISTORY_METHOD ,
2635 payload : { method, args } ,
2736 } ) ;
2837}
2938
30- export const push : ( ...args : Parameters < History [ 'push' ] > ) => ReduxAction = updateLocation ( 'push' ) ;
31- export const replace : ( ...args : Parameters < History [ 'replace' ] > ) => ReduxAction =
32- updateLocation ( 'replace' ) ;
33- export const go : ( ...args : Parameters < History [ 'go' ] > ) => ReduxAction = updateLocation ( 'go' ) ;
34- export const goBack : ( ) => ReduxAction = updateLocation ( 'goBack' ) ;
35- export const goForward : ( ) => ReduxAction = updateLocation ( 'goForward' ) ;
36- export const back : ( ) => ReduxAction = updateLocation ( 'back' ) ;
37- export const forward : ( ) => ReduxAction = updateLocation ( 'forward' ) ;
39+ export const push : (
40+ ...args : Parameters < History [ 'push' ] >
41+ ) => CallHistoryMethodAction < Parameters < History [ 'push' ] > > = updateLocation ( 'push' ) ;
42+ export const replace : (
43+ ...args : Parameters < History [ 'replace' ] >
44+ ) => CallHistoryMethodAction < Parameters < History [ 'replace' ] > > = updateLocation ( 'replace' ) ;
45+ export const go : (
46+ ...args : Parameters < History [ 'go' ] >
47+ ) => CallHistoryMethodAction < Parameters < History [ 'go' ] > > = updateLocation ( 'go' ) ;
48+ export const goBack : ( ) => CallHistoryMethodAction < Parameters < History [ 'goBack' ] > > =
49+ updateLocation ( 'goBack' ) ;
50+ export const goForward : ( ) => CallHistoryMethodAction < Parameters < History [ 'goForward' ] > > =
51+ updateLocation ( 'goForward' ) ;
52+ // @ts -ignore //support history 5.x back/forward
53+ export const back : ( ) => CallHistoryMethodAction < Parameters < History [ 'back' ] > > =
54+ updateLocation ( 'back' ) ;
55+ // @ts -ignore //support history 5.x back/forward
56+ export const forward : ( ) => CallHistoryMethodAction < Parameters < History [ 'forward' ] > > =
57+ updateLocation ( 'forward' ) ;
3858
3959export type RouterActions =
4060 | ReturnType < typeof push >
0 commit comments