import { Dispatch , SetStateAction , useState } from 'react' ; interface FactoryArguments < T > { state : T ; setState : Dispatch < SetStateAction < T > > ; } interface FactoryApi { [ key ] : any ; } export type TUseStateReducerFactory < T1 , T2 = FactoryApi > = ( args : FactoryArguments < T1 > ) => T2 ; export type TUseStateReducer < T1 , T2 = FactoryApi > = ( factory : TUseStateReducerFactory < T1 > , initialState : T1 ) => T2 ; export const useStateReducer = ( apiFactory , initialState ) => { const [ state , setState ] = useState ( initialState ) ; return apiFactory ( { state , setState } ) ; } ; export const useTStateReducer = < T1 , T2 = FactoryApi > ( apiFactory , initialState ) : T2 => { const [ state , setState ] = useState < T1 > ( initialState ) ; return apiFactory ( { state , setState } ) ; } ;