react-redux-methods
Version:
A lightweight react-redux toolkit for writing strong-typed, minimal code redux boilerplate.
51 lines (50 loc) • 3.19 kB
TypeScript
type InferEnhancer<TInjectedProps, TOwnProps = {}> = <TComponent extends React.ComponentType<TInjectedProps & TOwnProps>>(component: TComponent) => React.ComponentType<TOwnProps>;
export type ConnectorProps<TConnector> = TConnector extends InferEnhancer<infer TInjectedProps, any> ? TInjectedProps : never;
type MapSelectorReturnType<T> = {
[K in keyof T]: T[K] extends (...args: any[]) => infer R ? R : never;
};
type MapStateCallback<S> = (selectors: S) => Record<string, (state: any) => any>;
type MapDispatchCallback<A> = (actions: A) => Record<string, (...args: any[]) => any>;
export interface IReduxConnectorFn<S, A> {
/**
* ReduxConnector 'mapState' and 'mapDispatch' overload.
*
* This overload maps the selected state and action to the react component with typescript intellisense.
*/
<MS extends MapStateCallback<S>, MD extends MapDispatchCallback<A>, TOwnProps>(mapState: MS, mapDispatch: MD): InferEnhancer<MapSelectorReturnType<ReturnType<MS>> & ReturnType<MD>, TOwnProps>;
/**
* ReduxConnector 'mapState' overload.
*
* This overload maps the selected state to the react component with typescript intellisense.
*/
<MS extends MapStateCallback<S>, TOwnProps>(mapState: MS, mapDispatch?: null | undefined): InferEnhancer<MapSelectorReturnType<ReturnType<MS>>, TOwnProps>;
/**
* ReduxConnector 'mapDispatch' overload.
*
* This overload maps the selected action to the react component with typescript intellisense.
*/
<MD extends MapDispatchCallback<A>, TOwnProps>(mapState: null | undefined, mapDispatch: MD): InferEnhancer<ReturnType<MD>, TOwnProps>;
}
/**
* A utility function which simplifies redux's 'connect' boilerplate specifically when providing 'mapStateToProps' and/or 'mapDispatchToProps'
* parameters. This function extends typescript intelliSense for passing props to react component.
*
* @param mapState A callback function that accepts a single parameter 'selectors' and returns an object of the selected
* case reducer states or what we call 'mapStateToProps'. The parameter contains all use case selections provided during the creation of each case reducer (
* see 'createReduxMethods' function for more reference).
* @param mapDispatch Same to mapState, mapDispatch is a callback function with a single parameter-'actions' and returns an object
* called 'mapDispatchToProps'. This parameter provides the list of all available actions within redux context.
* @returns a function returned from react-redux's 'connect'.
*
* EXAMPLE:
* const connector = reduxConnector(
* (s) => ({
* savedSearchMutation: s.getSavedSearchMutation,
* accountingPeriodEntrySkipFetch: s.getAccountingPeriodEntrySkipFetch,
* currentUser: s.getCurrentUser,
* }),
* (a) => ({ openIsExport: a.openIsExport })
* );
*/
export declare const createConnector: <S, A>(selectors: S, actions: A) => <MS extends MapStateCallback<S>, MD extends MapDispatchCallback<A>>(mapState: MS, mapDispatch: MD) => import("react-redux").InferableComponentEnhancerWithProps<MapSelectorReturnType<ReturnType<MS>> & import("react-redux").ResolveThunks<ReturnType<MD>>, {}>;
export {};