Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve typing of StoreEnhancer #1578

Merged
merged 2 commits into from
Apr 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 7 additions & 5 deletions test/typescript/store.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
Store, createStore, Reducer, Action, StoreEnhancer,
StoreCreator, Unsubscribe
Store, createStore, Reducer, Action, StoreEnhancer, GenericStoreEnhancer,
StoreCreator, StoreEnhancerStoreCreator, 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 = <S>(next: StoreEnhancerStoreCreator<S>) => 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