I am having some inconvenience with typescript and redux-thunk effects.
Basically, my components actively use connect-redux to bind the creators of the action, the problem is that when I create an interface for these redux actions inside the component, I need to re-declare the function definition because it gets lost on the connection call.
Here is a sample code:
Action creator
import api from './api'; import { Dispatch } from 'redux'; import { IReduxAction } from 'model'; export const FETCH_ENTITY = 'location/FETCH_ENTITY'; export function fetchEntity(id: string) { return (dispatch: Dispatch<IReduxAction>) => { dispatch({type: `${FETCH_ENTITY}_REQUEST`, payload: {id}}); return api.fetchEntity(id) .then((res) => { dispatch({ type: `${FETCH_ENTITY}_DONE`, payload: res }); return res; }) .catch((err) => { dispatch({type: `${FETCH_ENTITY}_FAIL`, payload: err, error: true}); throw err; }); }; }
component
import * as React from 'react'; import { bindActionCreators } from 'redux'; import { connect } from 'react-redux'; import { fetchEntity, } from 'actions'; interface IStateProps { startingEntities: any[]; } interface IDispatchProps { fetchEntity: (id: string) => Promise<any>;
Is it possible to declare this created action object without having to re-declare it every time in every file?
source share