I am getting this typescript error: Property 'then' does not exist on type 'ThunkAction<Promise<boolean>, IinitialState, undefined, any>'.
Please help!
How I have configured my store and included the type:
return createStore(
rootReducer,
intialState,
require('redux-devtools-extension').composeWithDevTools(
applyMiddleware(
thunk as ThunkMiddleware<IinitialState, any>,
require('redux-immutable-state-invariant').default()
)
)
Action Creator:
type ThunkResult<R> = ThunkAction<R, IinitialState, undefined, any>;
export function anotherThunkAction(): ThunkResult<Promise<boolean>> {
return (dispatch, getState) => {
return Promise.resolve(true);
}
}
then in my component I have a prop interface:
interface IProps {
anotherThunkAction: typeof anotherThunkAction;
}
Then:
componentWillMount() {
this.props.anotherThunkAction().then(() => {console.log('hello world')})
}
Connect where I am using react-i18next as well:
export default translate('manageInventory')(
connect(
mapStateToProps,
{
anotherThunkAction
}
)(ManageInventory)
);
So glad to see redux-thunk has great support for Typescript and Redux v4. Where is the best place to view an up to date example of a correctly typed reducer, actions, and a connected component with redux-thunk?
Thunk and FSA promise middleware for Redux. It lets you work with thunks, promises or both. You get to choose what to use, depending on what you need. It lets you write sync & async action using the same syntax. The ability to dispatch other actions, like loading, and get information from the state (from thunks ).
If the solution happens to be easy for you, the main thing I am stuck on is how to return a promise in the action. type ThunkResult<R> = ThunkAction<R, IinitialState, undefined, any>; export function anotherThunkAction (): ThunkResult<Promise<boolean>> { return (dispatch, getState) => { return Promise.resolve (true); } }
The Promise’s reject () part is called in the following code since it always returns an error. So, .catch () will be executed. In TypeScript, promise chain-ability is the heart of promises’ benefits. Use the .then () function to create a chain of promises.
I don't think you're dispatching things properly... you are calling your action without passing it through the store.
If you call the action directly, you'll get back :
ThunkAction<Promise<boolean>, IinitialState, undefined, any>
Which as tsc is telling you, doesn't have a then
function. When you run something through dispatch
it turns ThunkResult<R>
into R
You haven't shown how you connect
your component to the store - but I think this is where the problem lies. Here is a sample:
type MyThunkDispatch = ThunkDispatch<IinitialState, undefined, any>
const mapDispatchToProps = (dispatch: MyThunkDispatch) => ({
anotherThunkAction: () => dispatch(anotherThunkAction())
})
connect(null, mapDispatchToProps)(MyComponent)
This will add anotherThunkAction
to props
and you can call it and it'll call into your action properly and return the promise.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With