import { Reducer , Action , combineReducers , ReducersMapObject } from '../..' ; /**
 * Simple reducer definition with no action shape checks.
 * Uses string comparison to determine action type.
 *
 * `AnyAction` type is used to allow action property access without requiring
 * type casting.
 */ function simple ( ) { type State = number ; const reducer = ( state = 0 , action ) => { if ( action . type === 'INCREMENT' ) { const { count = 1 } = action ; return state + count ; } if ( action . type === 'DECREMENT' ) { const { count = 1 } = action ; return state - count ; } return state ; } ; let s = reducer ( undefined , { type : 'init' } ) ; s = reducer ( s , { type : 'INCREMENT' } ) ; s = reducer ( s , { type : 'INCREMENT' , count : 10 } ) ; s = reducer ( s , { type : 'DECREMENT' } ) ; s = reducer ( s , { type : 'DECREMENT' , count : 10 } ) ; s = reducer ( s , { type : 'SOME_OTHER_TYPE' , someField : 'value' } ) ; reducer ( 'string' , { type : 'INCREMENT' } ) ; const combined = combineReducers ( { sub : reducer } ) ; let cs = combined ( undefined , { type : 'init' } ) ; cs = combined ( cs , { type : 'INCREMENT' , count : 10 } ) ; combined ( { unknown : '' } , { type : 'INCREMENT' } ) ; } /**
 * Reducer definition using discriminated unions.
 *
 * See https://basarat.gitbooks.io/typescript/content/docs/types/discriminated-unions.html#redux
 */ function discriminated ( ) { type State = number ; interface IncrementAction { type : 'INCREMENT' ; count ? : number ; } interface DecrementAction { type : 'DECREMENT' ; count ? : number ; } interface MultiplyAction { type : 'MULTIPLY' ; count ? : number ; } interface DivideAction { type : 'DIVIDE' ; count ? : number ; } type MyAction0 = IncrementAction | DecrementAction ; type MyAction1 = MultiplyAction | DivideAction ; const reducer0 = ( state = 0 , action ) => { if ( action . type === 'INCREMENT' ) { action . wrongField ; const { count = 1 } = action ; return state + count ; } if ( action . type === 'DECREMENT' ) { action . wrongField ; const { count = 1 } = action ; return state - count ; } return state ; } ; const reducer1 = ( state = 0 , action ) => { if ( action . type === 'MULTIPLY' ) { action . wrongField ; const { count = 1 } = action ; return state * count ; } if ( action . type === 'DIVIDE' ) { action . wrongField ; const { count = 1 } = action ; return state / count ; } return state ; } ; let s = reducer0 ( undefined , { type : 'init' } as any ) ; s = reducer0 ( s , { type : 'INCREMENT' } ) ; s = reducer0 ( s , { type : 'INCREMENT' , count : 10 } ) ; s = reducer0 ( s , { type : 'DECREMENT' , coun : 10 } ) ; s = reducer0 ( s , { type : 'DECREMENT' , count : 10 } ) ; s = reducer0 ( s , { type : 'SOME_OTHER_TYPE' } ) ; s = reducer0 ( s , { type : 'SOME_OTHER_TYPE' , someField : 'value' } ) ; const combined = combineReducers ( { sub0 : reducer0 , sub1 : reducer1 } ) ; const cs = combined ( undefined , { type : 'INCREMENT' } ) ; combined ( cs , { type : 'MULTIPLY' } ) ; combined ( cs , { type : 'init' } ) ; combined ( cs , { type : 'SOME_OTHER_TYPE' } ) ; const strictCombined = combineReducers < { sub : State ; } , MyAction0 > ( { sub : reducer0 } ) ; const scs = strictCombined ( undefined , { type : 'INCREMENT' } ) ; strictCombined ( scs , { type : 'DECREMENT' } ) ; strictCombined ( scs , { type : 'SOME_OTHER_TYPE' } ) ; } /**
 * Reducer definition using type guards.
 */ function typeGuards ( ) { function isAction < A extends Action > ( action , type ) { return action . type === type ; } type State = number ; interface IncrementAction { type : 'INCREMENT' ; count ? : number ; } interface DecrementAction { type : 'DECREMENT' ; count ? : number ; } const reducer = ( state = 0 , action ) => { if ( isAction < IncrementAction > ( action , 'INCREMENT' ) ) { action . wrongField ; const { count = 1 } = action ; return state + count ; } if ( isAction < DecrementAction > ( action , 'DECREMENT' ) ) { action . wrongField ; const { count = 1 } = action ; return state - count ; } return state ; } ; let s = reducer ( undefined , { type : 'init' } ) ; s = reducer ( s , { type : 'INCREMENT' } ) ; s = reducer ( s , { type : 'INCREMENT' , count : 10 } ) ; s = reducer ( s , { type : 'DECREMENT' } ) ; s = reducer ( s , { type : 'DECREMENT' , count : 10 } ) ; s = reducer ( s , { type : 'SOME_OTHER_TYPE' , someField : 'value' } ) ; const combined = combineReducers ( { sub : reducer } ) ; let cs = combined ( undefined , { type : 'init' } ) ; cs = combined ( cs , { type : 'INCREMENT' , count : 10 } ) ; } /**
 * Test ReducersMapObject with default type args.
 */ function reducersMapObject ( ) { const obj = { } ; for ( const key of Object . keys ( obj ) ) { obj [ key ] ( undefined , { type : 'SOME_TYPE' } ) ; obj [ key ] ( undefined , 'not-an-action' ) ; } }