我正在尝试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 …