如何让combineReducers使用flowtype?

问题描述 投票:0回答:1

我想使用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]

我的减速器只是一个采取状态和动作并返回一个新状态的函数。

然后我只是在reducer上调用combineReducers,如错误消息所示。

有人知道这个很容易解决吗?

redux flowtype
1个回答
2
投票

我找到了解决方案。

不知何故,你必须输入reducer的结果。你实际上可以做两件事,第一件事是:

export default combineReducers<*, *>({ message })

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

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

但是,这需要您跟踪您应该执行的状态和操作的类型。

© www.soinside.com 2019 - 2024. All rights reserved.