Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning a promise in a Redux thunk with typescript

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)
);
like image 290
jfbloom22 Avatar asked Aug 17 '18 15:08

jfbloom22


People also ask

Does Redux-thunk support TypeScript and Redux?

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?

What is thunk and promise middleware for Redux?

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 ).

How to return a promise from a thunk action?

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); } }

What happens when a promise returns an error in typescript?

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.


1 Answers

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.

like image 82
tswaters Avatar answered Oct 06 '22 14:10

tswaters