export interface AnyAction { type : any ; } export type Meta = null | { [ key ] : any ; } ; export interface Action < Payload > extends AnyAction { type : string ; payload : Payload ; error ? : boolean ; meta ? : Meta ; } example export function isType < Payload > ( action , actionCreator ) { return action . type === actionCreator . type ; } export interface ActionCreator < Payload > { type : string ; example match : ( action : AnyAction ) => action is Action < Payload > ; param payload param meta ( payload , meta ? ) : Action < Payload > ; } export type Success < Params , Result > = ( { params : Params ; } | ( Params extends void ? { params ? : Params ; } : never ) ) & ( { result : Result ; } | ( Result extends void ? { result ? : Result ; } : never ) ) ; export type Failure < Params , Error > = ( { params : Params ; } | ( Params extends void ? { params ? : Params ; } : never ) ) & { error : Error ; } ; export interface AsyncActionCreators < Params , Result , Error = { } > { type : string ; started : ActionCreator < Params > ; done : ActionCreator < Success < Params , Result > > ; failed : ActionCreator < Failure < Params , Error > > ; } export interface ActionCreatorFactory { param type param commonMeta param isError < Payload = void > ( type , commonMeta ? , isError ? ) : ActionCreator < Payload > ; param type param commonMeta param isError < Payload = void > ( type , commonMeta ? , isError ? ) : ActionCreator < Payload > ; param type param commonMeta async < Params , Result , Error = { } > ( type , commonMeta ? ) : AsyncActionCreators < Params , Result , Error > ; } declare const process ; param prefix param defaultIsError export function actionCreatorFactory ( prefix ? , defaultIsError = p => p instanceof Error ) { const actionTypes = { } ; const base = prefix ? `${ prefix }/` : '' ; function actionCreator < Payload > ( type , commonMeta ? , isError = defaultIsError ) { const fullType = base + type ; if ( process . env . NODE_ENV !== 'production' ) { if ( actionTypes [ fullType ] ) throw new Error ( `Duplicate action type: ${ fullType }` ) ; actionTypes [ fullType ] = true ; } return Object . assign ( ( payload , meta ? ) => { const action = { type : fullType , payload } ; if ( commonMeta || meta ) { action . meta = Object . assign ( { } , commonMeta , meta ) ; } if ( isError && ( typeof isError === 'boolean' || isError ( payload ) ) ) { action . error = true ; } return action ; } , { type : fullType , toString : ( ) => fullType , match : ( action ) : action is Action < Payload > => action . type === fullType } ) as ActionCreator < Payload > ; } function asyncActionCreators < Params , Result , Error > ( type , commonMeta ? ) { return { type : base + type , started : actionCreator < Params > ( `${ type }_STARTED` , commonMeta , false ) , done : actionCreator < Success < Params , Result > > ( `${ type }_DONE` , commonMeta , false ) , failed : actionCreator < Failure < Params , Error > > ( `${ type }_FAILED` , commonMeta , true ) } ; } return Object . assign ( actionCreator , { async : asyncActionCreators } ) ; } export default actionCreatorFactory ;