11import compose from './compose'
2+ import {
3+ Middleware ,
4+ StoreEnhancer ,
5+ StoreCreator ,
6+ AnyAction ,
7+ Reducer ,
8+ Dispatch ,
9+ MiddlewareAPI
10+ } from '..'
211
312/**
413 * Creates a store enhancer that applies middleware to the dispatch method
@@ -16,19 +25,24 @@ import compose from './compose'
1625 * @param {...Function } middlewares The middleware chain to be applied.
1726 * @returns {Function } A store enhancer applying the middleware.
1827 */
19- export default function applyMiddleware ( ...middlewares ) {
20- return createStore => ( ...args ) => {
21- const store = createStore ( ...args )
22- let dispatch = ( ) => {
28+ export default function applyMiddleware (
29+ ...middlewares : Middleware [ ]
30+ ) : StoreEnhancer {
31+ return ( createStore : StoreCreator ) => < S , A extends AnyAction > (
32+ reducer : Reducer < S , A > ,
33+ ...args : any [ ]
34+ ) => {
35+ const store = createStore ( reducer , ...args )
36+ let dispatch : Dispatch = ( ) => {
2337 throw new Error (
2438 'Dispatching while constructing your middleware is not allowed. ' +
2539 'Other middleware would not be applied to this dispatch.'
2640 )
2741 }
2842
29- const middlewareAPI = {
43+ const middlewareAPI : MiddlewareAPI = {
3044 getState : store . getState ,
31- dispatch : ( ...args ) => dispatch ( ...args )
45+ dispatch : ( action , ...args ) => dispatch ( action , ...args )
3246 }
3347 const chain = middlewares . map ( middleware => middleware ( middlewareAPI ) )
3448 dispatch = compose ( ...chain ) ( store . dispatch )
0 commit comments