Skip to content

Commit f8439c6

Browse files
committed
Merge pull request reduxjs#1578 from Igorbek/ts-def-improv
Improve typing of StoreEnhancer
2 parents 3388903 + 9db3dd4 commit f8439c6

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

index.d.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,8 @@ export interface Store<S> {
194194
* @template S State object type.
195195
*/
196196
export interface StoreCreator {
197-
<S>(reducer: Reducer<S>, enhancer?: StoreEnhancer): Store<S>;
198-
<S>(reducer: Reducer<S>, initialState: S,
199-
enhancer?: StoreEnhancer): Store<S>;
197+
<S>(reducer: Reducer<S>, enhancer?: StoreEnhancer<S>): Store<S>;
198+
<S>(reducer: Reducer<S>, initialState: S, enhancer?: StoreEnhancer<S>): Store<S>;
200199
}
201200

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

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

292293

293294
/* action creators */

test/typescript/store.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
2-
Store, createStore, Reducer, Action, StoreEnhancer,
3-
StoreCreator, Unsubscribe
2+
Store, createStore, Reducer, Action, StoreEnhancer, GenericStoreEnhancer,
3+
StoreCreator, StoreEnhancerStoreCreator, Unsubscribe
44
} from "../../index.d.ts";
55

66

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

24-
const enhancer: StoreEnhancer = (next: StoreCreator) => next;
24+
const genericEnhancer: GenericStoreEnhancer = <S>(next: StoreEnhancerStoreCreator<S>) => next;
25+
const specificEnhencer: StoreEnhancer<State> = next => next;
2526

26-
const storeWithEnhancer: Store<State> = createStore(reducer, enhancer);
27+
const storeWithGenericEnhancer: Store<State> = createStore(reducer, genericEnhancer);
28+
const storeWithSpecificEnhancer: Store<State> = createStore(reducer, specificEnhencer);
2729

2830
const storeWithInitialStateAndEnhancer: Store<State> = createStore(reducer, {
2931
todos: []
30-
}, enhancer);
32+
}, genericEnhancer);
3133

3234

3335
/* dispatch */

0 commit comments

Comments
 (0)