import type { Action } from 'redux' ; import type { IsUnknownOrNonInferrable , IfMaybeUndefined , IfVoid , IsAny , } from './tsHelpers' ; import isPlainObject from './isPlainObject' ; template P template T template M template E public export type PayloadAction < P = void , T extends string = string , M = never , E = never > = { payload : P ; type : T ; } & ( [ M ] extends [ never ] ? { } : { meta : M ; } ) & ( [ E ] extends [ never ] ? { } : { error : E ; } ) ; public export type PrepareAction < P > = ( ( ... args : any [ ] ) => { payload : P ; } ) | ( ( ... args : any [ ] ) => { payload : P ; meta : any ; } ) | ( ( ... args : any [ ] ) => { payload : P ; error : any ; } ) | ( ( ... args : any [ ] ) => { payload : P ; meta : any ; error : any ; } ) ; internal export type _ActionCreatorWithPreparedPayload < PA extends PrepareAction < any > | void , T extends string = string > = PA extends PrepareAction < infer P > ? ActionCreatorWithPreparedPayload < Parameters < PA > , P , T , ReturnType < PA > extends { error : infer E ; } ? E : never , ReturnType < PA > extends { meta : infer M ; } ? M : never > : void ; inheritdoc interface BaseActionCreator < P , T extends string , M = never , E = never > { type : T ; match : ( action : Action < unknown > ) => action is PayloadAction < P , T , M , E > ; } typeParam typeParam typeParam typeParam typeParam inheritdoc public export interface ActionCreatorWithPreparedPayload < Args extends unknown [ ] , P , T extends string = string , E = never , M = never > extends BaseActionCreator < P , T , M , E > { /**
     * Calling this  redux with `Args` will return
     * an Action with a payload of type `P` and (depending on the `PrepareAction`
     * method used) a `meta`- and `error` property of types `M` and `E` respectively.
      ( ... args ) : PayloadAction < P , T , M , E > ; } inheritdoc public export interface ActionCreatorWithOptionalPayload < P , T extends string = string > extends BaseActionCreator < P , T > { /**
     * Calling this  redux with an argument will
     * return a  PayloadAction of type `T` with a payload of `P`.
     * Calling it without an argument will return a PayloadAction with a payload of `undefined`.
      ( payload ? ) : PayloadAction < P , T > ; } inheritdoc public export interface ActionCreatorWithoutPayload < T extends string = string > extends BaseActionCreator < undefined , T > { /**
     * Calling this  redux will
     * return a  PayloadAction of type `T` with a payload of `undefined`
      ( ) : PayloadAction < undefined , T > ; } inheritdoc public export interface ActionCreatorWithPayload < P , T extends string = string > extends BaseActionCreator < P , T > { /**
     * Calling this  redux with an argument will
     * return a  PayloadAction of type `T` with a payload of `P`
      ( payload ) : PayloadAction < P , T > ; } inheritdoc public export interface ActionCreatorWithNonInferrablePayload < T extends string = string > extends BaseActionCreator < unknown , T > { /**
     * Calling this  redux with an argument will
     * return a  PayloadAction of type `T` with a payload
     * of exactly the type of the argument.
      < PT extends unknown > ( payload ) : PayloadAction < PT , T > ; } typeParam typeParam typeParam public export type PayloadActionCreator < P = void , T extends string = string , PA extends PrepareAction < P > | void = void > = IfPrepareActionMethodProvided < PA , _ActionCreatorWithPreparedPayload < PA , T > , IsAny < P , ActionCreatorWithPayload < any , T > , IsUnknownOrNonInferrable < P , ActionCreatorWithNonInferrablePayload < T > , IfVoid < P , ActionCreatorWithoutPayload < T > , IfMaybeUndefined < P , ActionCreatorWithOptionalPayload < P , T > , ActionCreatorWithPayload < P , T > > > > > > ; param type param prepare public export function createAction < P = void , T extends string = string > ( type ) ; param type param prepare public export function createAction < PA extends PrepareAction < any > , T extends string = string > ( type , prepareAction ) ; export function createAction ( type , prepareAction ? ) { function actionCreator ( ... args ) { if ( prepareAction ) { let prepared = prepareAction ( ... args ) ; if ( ! prepared ) { throw new Error ( 'prepareAction did not return an object' ) ; } return { type , payload : prepared . payload , ... ( 'meta' in prepared && { meta : prepared . meta } ) , ... ( 'error' in prepared && { error : prepared . error } ) , } ; } return { type , payload : args [ 0 ] } ; } actionCreator . toString = ( ) => `${ type }` ; actionCreator . type = type ; actionCreator . match = ( action ) : action is PayloadAction => action . type === type ; return actionCreator ; } export function isFSA ( action ) { return ( isPlainObject ( action ) && typeof ( action as any ) . type === 'string' && Object . keys ( action ) . every ( isValidKey ) ) ; } function isValidKey ( key ) { return [ 'type' , 'payload' , 'error' , 'meta' ] . indexOf ( key ) > - 1 ; } param action returns public export function getType < T extends string > ( actionCreator ) { return `${ actionCreator }` as T ; } type IfPrepareActionMethodProvided < PA extends PrepareAction < any > | void , True , False > = PA extends ( ... args : any [ ] ) => any ? True : False ;