简体   繁体   English

使用flowtype和flow-typed输入redux存储

[英]typing redux store using flowtype and flow-typed

I'm trying to type the redux store like this : const s:Store<S,A>=createStore (todoApp) but I get 我正在尝试像这样键入redux存储const s:Store<S,A>=createStore (todoApp)但我得到了

identifier Store ... Could not resolve name flow error identifier Store ... Could not resolve name流错误

any idea how to fix this ? 任何想法如何解决这个问题?

I am using this flow typed declarations: 我正在使用此流类型的声明:

// 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;

}

You need to import the type Store from the redux module. 您需要从redux模块导入类型Store Example: 例:

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

// ...

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

For more information, you can refer to the documentation for importing/exporting types from modules . 有关更多信息,您可以参考有关从模块导入/导出类型文档

This is an example how you can type reducers and store: (using redux v4 and flow v0.108) 这是一个示例,您可以键入reducer并存储:(使用redux v4和flow v0.108)

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM