javascript - 使用 flowtype 和 flow-typed 输入 redux store

标签 javascript redux flowtype

我正在尝试 to type the redux store像这样:const s:Store<S,A>=createStore (todoApp)但我明白了

identifier Store ... Could not resolve name流量错误

知道如何解决这个问题吗?

我正在使用这个流类型的声明:

// flow-typed signature: ba132c96664f1a05288f3eb2272a3c35
// flow-typed version: c4bbd91cfc/redux_v3.x.x/flow_>=v0.33.x

declare module 'redux' {

  /*

    S = State
    A = Action

  */

  declare type Dispatch<A: { type: $Subtype<string> }> = (action: A) => A;

  declare type MiddlewareAPI<S, A> = {
    dispatch: Dispatch<A>;
    getState(): S;
  };

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

  declare type Reducer<S, A> = (state: S, action: A) => S;

  declare type Middleware<S, A> =
    (api: MiddlewareAPI<S, A>) =>
      (next: Dispatch<A>) => Dispatch<A>;

  declare type StoreCreator<S, A> = {
    (reducer: Reducer<S, A>, enhancer?: StoreEnhancer<S, A>): Store<S, A>;
    (reducer: Reducer<S, A>, preloadedState: S, enhancer?: StoreEnhancer<S, A>): Store<S, A>;
  };

  declare type StoreEnhancer<S, A> = (next: StoreCreator<S, A>) => StoreCreator<S, A>;

  declare function createStore<S, A>(reducer: Reducer<S, A>, enhancer?: StoreEnhancer<S, A>): Store<S, A>;
  declare function createStore<S, A>(reducer: Reducer<S, A>, preloadedState: S, enhancer?: StoreEnhancer<S, A>): Store<S, A>;

  declare function applyMiddleware<S, A>(...middlewares: Array<Middleware<S, A>>): StoreEnhancer<S, A>;

  declare type ActionCreator<A, B> = (...args: Array<B>) => A;
  declare type ActionCreators<K, A> = { [key: K]: ActionCreator<A, any> };

  declare function bindActionCreators<A, C: ActionCreator<A, any>>(actionCreator: C, dispatch: Dispatch<A>): C;
  declare function bindActionCreators<A, K, C: ActionCreators<K, A>>(actionCreators: C, dispatch: Dispatch<A>): C;

  declare function combineReducers<O: Object, A>(reducers: O): Reducer<$ObjMap<O, <S>(r: Reducer<S, any>) => S>, A>;

  declare function compose<S, A>(...fns: Array<StoreEnhancer<S, A>>): Function;

}

最佳答案

您需要从 redux 模块导入 Store 类型。示例:

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

// ...

const s: Store<S, A> = createStore(todoApp) 

更多信息可以引用documentation for importing/exporting types from modules .

关于javascript - 使用 flowtype 和 flow-typed 输入 redux store,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40702834/

相关文章:

javascript - 每 30 秒执行一次函数 jquery mobile?

javascript - redux中isPlainObject函数的实现

cors - 调用axios.get时发生CORS错误

javascript - 使用 npm 包导出我自己的流类型?

Javascript:重新加载 DIV 后按钮被禁用

javascript - 在 D3.js 饼图上显示标签

javascript - 如何限制 HighCharts 中的图表 xAxis 日期范围?

reactjs - 在React js中使用redux表单时出现字段错误

javascript - 将流类型 SuperType 的对象分配给 SubType 会导致错误

javascript - 使用流优化联合类型中的可选属性