如何使用 flow-typed 类型化 Redux store?

How to use flow-typed to type Redux store?

我正在尝试使用流式正确键入 Redux。 我使用从 redux 导入的 Store 像这样输入我的商店,Flow 使用 flow-typed 文件 redux_v4.x.x.js 并给我一个错误:

/* @flow */
import rootReducer from '@reducers/index'
import type { Store } from 'redux'
import { applyMiddleware, createStore } from 'redux'
import { composeWithDevTools } from 'redux-devtools-extension'
import thunk from 'redux-thunk'

const configureStore = () => {
   const store: Store = createStore(
    rootReducer,
    composeWithDevTools(applyMiddleware(thunk))
  )
  return store
}

export { configureStore as default }

使用此代码,我得到以下错误:Cannot use Store [1] without 2-3 type arguments

我也试过这样定义它const store: Store<S, A>但是 Flow 抱怨说他没有找到 S 和 A 的声明,这看起来很正常。

flow-typed使用的redux_v4.x.x.js文件中,Store是这样定义的:

  declare export type Store<S, A, D = Dispatch<A>> = {
    // rewrite MiddlewareAPI members in order to get nicer error messages (intersections produce long messages)
    dispatch: D,
    getState(): S,
    subscribe(listener: () => void): () => void,
    replaceReducer(nextReducer: Reducer<S, A>): void,
  };

这个post我已经看过了,和我的很像,但是至今没有答案,所以这个post就像一个凸起

感谢帮助!

您需要为 Store 类型提供 SA。例如,

import { createStore } from 'redux';
import type { Store } from 'redux';

type State = Array<number>;
type Action = { type: 'A' };
type MyStore = Store<State, Action>;
const store: MyStore = createStore(...);

// or inline
const store: Store<Array<number>, { type: 'A' }> = createStore(...);

查看 test_createStore.js or, for a more complete example, check out this sandbox 中提供的示例。