首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用flow类型和flow类型输入redux存储

使用flow类型和flow类型输入redux存储
EN

Stack Overflow用户
提问于 2016-11-20 10:13:46
回答 2查看 3.8K关注 0票数 6

我正在尝试这样的键入redux存储区const s:Store<S,A>=createStore (todoApp),但我得到了

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

知道怎么解决这个问题吗?

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

代码语言:javascript
复制
// 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;

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-11-20 17:26:31

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

代码语言:javascript
复制
import { createStore } from 'redux';
import type { Store } from 'redux';

// ...

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

有关更多信息,您可以参考从模块导入/导出类型的文档

票数 14
EN

Stack Overflow用户

发布于 2019-10-15 09:14:06

这是一个如何键入还原器和存储的示例:(使用redux v4和flow v0.108)

代码语言:javascript
复制
import { createStore, combineReducers } from 'redux';
import type { Action, Store } from 'redux';

type TodoState = $Exact<{
  +text: string;
  +completed: boolean;
  +id: number;
}>;

type TodoListState = TodoState[];
type VisibilityFilterState = 'SHOW_ACTIVE' | 'SHOW_ALL' | 'SHOW_COMPLETED';

const todoListReducer = (state: (TodoListState | void) = [], action: Action<any>): TodoListState => {
  // reducer code here    
};

const visibilityFilterReducer = (state: (VisibilityFilterState | void) = 'SHOW_ALL', action: Action<any>) : VisibilityFilterState =>  {
  // reducer code here
}

type AppState = $Exact<{
  todoListState: TodoListState,
  visibilityFilterState: VisibilityFilterState
}>;

const appStateReducer: (AppState | void, Action<any>) => AppState = combineReducers({
  todoListState: todoListReducer,
  visibilityFilterState: visibilityFilterReducer
});

const store: Store<AppState, Action<any> = createStore(appStateReducer);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40702834

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档