相关疑难解决方法(0)

如何在 Typescript 中使用 redux-thunk 使用 ThunkAction 正确键入 thunk?

我正在尝试redux-thunk使用 Typescript键入检查我的代码。

从 Redux: Usage with Redux Thunk的官方文档中,我们得到了这个例子:

// src/thunks.ts

import { Action } from 'redux'
import { sendMessage } from './store/chat/actions'
import { RootState } from './store'
import { ThunkAction } from 'redux-thunk'

export const thunkSendMessage = (
  message: string
): ThunkAction<void, RootState, unknown, Action<string>> => async dispatch => {
  const asyncResp = await exampleAPI()
  dispatch(
    sendMessage({
      message,
      user: asyncResp,
      timestamp: new Date().getTime()
    })
  )
}

function exampleAPI() {
  return Promise.resolve('Async Chat Bot')
}
Run Code Online (Sandbox Code Playgroud)

为了减少重复,您可能希望在您的商店文件中定义一次可重用的 AppThunk …

typescript redux redux-thunk typescript-typings

3
推荐指数
1
解决办法
3258
查看次数