Skip to content

Commit

Permalink
Improve typing of StoreEnhancer
Browse files Browse the repository at this point in the history
  • Loading branch information
Igorbek committed Apr 5, 2016
1 parent 3388903 commit 324849d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
11 changes: 6 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,8 @@ export interface Store<S> {
* @template S State object type.
*/
export interface StoreCreator {
<S>(reducer: Reducer<S>, enhancer?: StoreEnhancer): Store<S>;
<S>(reducer: Reducer<S>, initialState: S,
enhancer?: StoreEnhancer): Store<S>;
<S>(reducer: Reducer<S>, enhancer?: StoreEnhancer<S>): Store<S>;
<S>(reducer: Reducer<S>, initialState: S, enhancer?: StoreEnhancer<S>): Store<S>;
}

/**
Expand All @@ -217,7 +216,9 @@ export interface StoreCreator {
* without the app being aware it is happening. Amusingly, the Redux
* middleware implementation is itself a store enhancer.
*/
export type StoreEnhancer = (next: StoreCreator) => StoreCreator;
export type StoreEnhancer<S> = (next: StoreEnhancerStoreCreator<S>) => StoreEnhancerStoreCreator<S>;
export type GenericStoreEnhancer = <S>(next: StoreEnhancerStoreCreator<S>) => StoreEnhancerStoreCreator<S>;
export type StoreEnhancerStoreCreator<S> = (reducer: Reducer<S>, initialState: S) => Store<S>;

/**
* Creates a Redux store that holds the state tree.
Expand Down Expand Up @@ -287,7 +288,7 @@ export interface Middleware {
* @param middlewares The middleware chain to be applied.
* @returns A store enhancer applying the middleware.
*/
export function applyMiddleware(...middlewares: Middleware[]): StoreEnhancer;
export function applyMiddleware(...middlewares: Middleware[]): GenericStoreEnhancer;


/* action creators */
Expand Down
10 changes: 6 additions & 4 deletions test/typescript/store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
Store, createStore, Reducer, Action, StoreEnhancer,
Store, createStore, Reducer, Action, StoreEnhancer, GenericStoreEnhancer,
StoreCreator, Unsubscribe
} from "../../index.d.ts";

Expand All @@ -21,13 +21,15 @@ const storeWithInitialState: Store<State> = createStore(reducer, {
todos: []
});

const enhancer: StoreEnhancer = (next: StoreCreator) => next;
const genericEnhancer: GenericStoreEnhancer = next => next;
const specificEnhencer: StoreEnhancer<State> = next => next;

const storeWithEnhancer: Store<State> = createStore(reducer, enhancer);
const storeWithGenericEnhancer: Store<State> = createStore(reducer, genericEnhancer);
const storeWithSpecificEnhancer: Store<State> = createStore(reducer, specificEnhencer);

const storeWithInitialStateAndEnhancer: Store<State> = createStore(reducer, {
todos: []
}, enhancer);
}, genericEnhancer);


/* dispatch */
Expand Down

0 comments on commit 324849d

Please sign in to comment.