@@ -641,14 +641,16 @@ export const INCREMENT_SFC = 'INCREMENT_SFC';
641
641
export const DECREMENT_SFC = ' DECREMENT_SFC' ;
642
642
643
643
export type Actions = {
644
- INCREMENT_SFC: { type: typeof INCREMENT_SFC },
645
- DECREMENT_SFC: { type: typeof DECREMENT_SFC },
644
+ INCREMENT_SFC: {
645
+ type: typeof INCREMENT_SFC ,
646
+ },
647
+ DECREMENT_SFC: {
648
+ type: typeof DECREMENT_SFC ,
649
+ },
646
650
};
647
651
648
- export type Action = Actions [keyof Actions ];
649
-
650
652
// Action Creators
651
- export const actionCreators = {
653
+ export const actionCreatorss = {
652
654
incrementSfc : (): Actions [typeof INCREMENT_SFC ] => ({
653
655
type: INCREMENT_SFC ,
654
656
}),
@@ -680,20 +682,24 @@ A more DRY approach, introducing a simple factory function to automate the creat
680
682
``` ts
681
683
import { createActionCreator } from ' react-redux-typescript' ;
682
684
685
+ type Severity = ' info' | ' success' | ' warning' | ' error' ;
686
+
683
687
// Action Creators
684
688
export const actionCreators = {
685
- increaseCounter: createActionCreator (' INCREASE_COUNTER' ), // { type: "INCREASE_COUNTER" }
686
- showNotification: createActionCreator (' SHOW_NOTIFICATION' , (payload : string , meta ? : { severity: string }) => payload ),
689
+ incrementCounter: createActionCreator (' INCREMENT_COUNTER' ),
690
+ showNotification: createActionCreator (
691
+ ' SHOW_NOTIFICATION' , (message : string , severity ? : Severity ) => ({ message , severity }),
692
+ ),
687
693
};
688
694
689
695
// Examples
690
- store .dispatch (actionCreators .increaseCounter (4 )); // Error: Expected 0 arguments, but got 1.
691
- store .dispatch (actionCreators .increaseCounter ()); // OK: { type: "INCREASE_COUNTER " }
692
- actionCreators .increaseCounter .type === " INCREASE_COUNTER " // true
696
+ store .dispatch (actionCreators .incrementCounter (4 )); // Error: Expected 0 arguments, but got 1.
697
+ store .dispatch (actionCreators .incrementCounter ()); // OK: { type: "INCREMENT_COUNTER " }
698
+ actionCreators .incrementCounter .type === " INCREMENT_COUNTER " // true
693
699
694
700
store .dispatch (actionCreators .showNotification ()); // Error: Supplied parameters do not match any signature of call target.
695
- store .dispatch (actionCreators .showNotification (' Hello!' )); // OK: { type: "SHOW_NOTIFICATION", payload: 'Hello!' }
696
- store .dispatch (actionCreators .showNotification (' Hello!' , { severity: ' warning ' } )); // OK: { type: "SHOW_NOTIFICATION", payload: 'Hello!', meta: { type: 'warning' } }
701
+ store .dispatch (actionCreators .showNotification (' Hello!' )); // OK: { type: "SHOW_NOTIFICATION", payload: { message: 'Hello!' } }
702
+ store .dispatch (actionCreators .showNotification (' Hello!' , ' info ' )); // OK: { type: "SHOW_NOTIFICATION", payload: { message: 'Hello!', severity: 'info } }
697
703
actionCreators .showNotification .type === " SHOW_NOTIFICATION" // true
698
704
```
699
705
@@ -813,17 +819,17 @@ export default function reducer(state = 0, action: RootAction): State {
813
819
// RootActions
814
820
import { RouterAction , LocationChangeAction } from ' react-router-redux' ;
815
821
816
- import { Action as CountersAction } from ' @src/redux/counters' ;
817
- import { Action as TodosAction } from ' @src/redux/todos' ;
818
- import { Action as ToastsAction } from ' @src/redux/toasts' ;
822
+ import { Actions as CountersActions } from ' @src/redux/counters' ;
823
+ import { Actions as TodosActions } from ' @src/redux/todos' ;
824
+ import { Actions as ToastsActions } from ' @src/redux/toasts' ;
819
825
820
826
type ReactRouterAction = RouterAction | LocationChangeAction ;
821
827
822
828
export type RootAction =
823
829
| ReactRouterAction
824
- | CountersAction
825
- | TodosAction
826
- | ToastsAction ;
830
+ | CountersActions [ keyof CountersActions ]
831
+ | TodosActions [ keyof TodosActions ]
832
+ | ToastsActions [ keyof ToastsActions ] ;
827
833
828
834
```
829
835
0 commit comments