【发布时间】:2019-06-23 20:50:45
【问题描述】:
我正在尝试在基于 TypeScript 的 React Native 项目中使用 redux-thunk。我的操作被定义为
export type Action<T> = {
type: T,
payload?: any
}
我的店铺定义为
import { applyMiddleware, createStore } from 'redux'
import { persistStore } from 'redux-persist'
import thunk from 'redux-thunk'
import logger from 'src/redux/middleware/logger'
import rootReducer from 'src/redux/reducers'
export const store = createStore(rootReducer, applyMiddleware(logger, thunk))
export const persistor = persistStore(store)
我的动作构造函数定义为
export const setRepeat = (repeat: Repeating) => (
dispatch: Dispatch<Action>,
getState: () => AppState
) =>
dispatch<Action>({
payload: repeat,
type: actionTypes.SET_REPEAT,
})
Repeating 是一个简单的枚举:
export enum Repeating {
All = 'all',
None = 'none',
Single = 'single',
}
当我尝试用
发送这个动作时store.dispatch(setRepeat('all' as Repeating))
我收到此错误
TS2345: Property 'type' is missing in type '(dispatch: Dispatch<Action<string>>, getState: () => AppState) => Action<string>' but required in type 'Action<string>'.
据我所知,这意味着setRepeat 正在返回一个函数(这是有道理的,这就是它的定义)。但是store.dispatch 并不期待像 redux-thunk 这样的函数允许并且仍然期待 Action。
我似乎找不到任何解释为什么 thunk 在这里没有提供正确的类型,或者我需要做些什么来纠正这个问题。
- 反应原生:0.57.7
- redux:4.0.1
- react-redux: 6.0.0
- redux-thunk:2.3.0
【问题讨论】:
-
能否提供Repeating接口?我知道出了什么问题,但需要该代码来确认,谢谢。
-
是的,我把它加进去了。
标签: typescript react-native redux-thunk