如何让 combineReducers 与 flowtype 一起工作?

Arw*_*ett 2 flowtype redux

我想使用 redux 中的 combineReducers 函数。但是我收到以下错误消息:

Missing type annotation for `A`. `A` is a type parameter declared in function type [1] and was implicitly instantiated
at call of `combineReducers` [2].

   src/reducer/index.js:12:16
   12| export default combineReducers({ message })
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ [2]

References:
   flow-typed/npm/redux_v4.x.x.js:56:42
   56|   declare export function combineReducers<O: Object, A>(reducers: O): CombinedReducer<$ObjMap<O, <S>(r: Reducer<S, any>) => S>, A>;
                                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ [1]
Run Code Online (Sandbox Code Playgroud)

我的 reducer 只是一个接受状态和动作并返回新状态的函数。

然后我只调用combineReducers减速器,如错误消息所示。

有人知道一个简单的解决方法吗?

Arw*_*ett 5

我找到了解决办法。

您必须以某种方式键入减速器的结果。你实际上可以做两件事,第一件事是:

export default combineReducers<*, *>({ message })
Run Code Online (Sandbox Code Playgroud)

对我来说,这感觉就像一个黑客。更好的解决方案是:

type State = {
    message: MessageState
}
const reducers: Reducer<State, Action> = combineReducers({ message })
export default reducers
Run Code Online (Sandbox Code Playgroud)

但是,这需要您跟踪状态和操作的类型,无论如何您都应该这样做。