Skip to content

Simplify applyMiddleware #3570

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

Closed
wants to merge 1 commit into from
Closed
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
53 changes: 16 additions & 37 deletions src/applyMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import compose from './compose'
import { Middleware, MiddlewareAPI } from './types/middleware'
import { AnyAction } from './types/actions'
import { StoreEnhancer, StoreCreator, Dispatch } from './types/store'
import { Reducer } from './types/reducers'
import {
Dispatch,
StoreEnhancer,
StoreEnhancerStoreCreator
} from './types/store'

/**
* Creates a store enhancer that applies middleware to the dispatch method
Expand All @@ -23,40 +25,17 @@ import { Reducer } from './types/reducers'
* @template Ext Dispatch signature added by a middleware.
* @template S The type of the state supported by a middleware.
*/
export default function applyMiddleware(): StoreEnhancer
export default function applyMiddleware<Ext1, S>(
middleware1: Middleware<Ext1, S, any>
): StoreEnhancer<{ dispatch: Ext1 }>
export default function applyMiddleware<Ext1, Ext2, S>(
middleware1: Middleware<Ext1, S, any>,
middleware2: Middleware<Ext2, S, any>
): StoreEnhancer<{ dispatch: Ext1 & Ext2 }>
export default function applyMiddleware<Ext1, Ext2, Ext3, S>(
middleware1: Middleware<Ext1, S, any>,
middleware2: Middleware<Ext2, S, any>,
middleware3: Middleware<Ext3, S, any>
): StoreEnhancer<{ dispatch: Ext1 & Ext2 & Ext3 }>
export default function applyMiddleware<Ext1, Ext2, Ext3, Ext4, S>(
middleware1: Middleware<Ext1, S, any>,
middleware2: Middleware<Ext2, S, any>,
middleware3: Middleware<Ext3, S, any>,
middleware4: Middleware<Ext4, S, any>
): StoreEnhancer<{ dispatch: Ext1 & Ext2 & Ext3 & Ext4 }>
export default function applyMiddleware<Ext1, Ext2, Ext3, Ext4, Ext5, S>(
middleware1: Middleware<Ext1, S, any>,
middleware2: Middleware<Ext2, S, any>,
middleware3: Middleware<Ext3, S, any>,
middleware4: Middleware<Ext4, S, any>,
middleware5: Middleware<Ext5, S, any>
): StoreEnhancer<{ dispatch: Ext1 & Ext2 & Ext3 & Ext4 & Ext5 }>
export default function applyMiddleware<Ext, S = any>(
...middlewares: Middleware<any, S, any>[]
): StoreEnhancer<{ dispatch: Ext }>
export default function applyMiddleware(
...middlewares: Middleware[]
): StoreEnhancer<any> {
return (createStore: StoreCreator) => <S, A extends AnyAction>(
reducer: Reducer<S, A>,
export default function applyMiddleware<
S = any,
M extends Middleware = Middleware
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this need to be generic? In what case would our middleware not be a Middleware? Either we don't need this, or we need to add a test for this generic.

Copy link
Author

@jednano jednano Sep 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's not generic, I can't infer off of it; rather, I do get errors if I try to do typeof middlewares extends Middleware<any, any, infer D>.

Type '(createStore: StoreEnhancerStoreCreator<any, never>) => <S = any, A extends Action<any> = AnyAction>(reducer: Reducer<S, A>, ...args: any[]) => any' is not assignable to type 'StoreEnhancer<never, S>'.
  Type '<S = any, A extends Action<any> = AnyAction>(reducer: Reducer<S, A>, ...args: any[]) => any' is not assignable to type 'StoreEnhancerStoreCreator<never, S>'.
    Type 'any' is not assignable to type 'never'.ts(2322)

It will always be a Middleware, which is why I said it must extend Middleware, but there's no reason someone can't create an interface which extends Middleware and use that too.

Copy link
Author

@jednano jednano Sep 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, we already have tests against this generic in the existing applyMiddleware.spec.ts which pass just fine with npm run test:cov for some reason; though, I am seeing red underlines in my editor (even before my changes here). I've fixed those red-line errors in other pieces of my work, but once I started breaking things apart, it's a bit harder to compose these things into smaller chunks w/o some overlap.

>(
...middlewares: M[]
): StoreEnhancer<
M extends Middleware<any, any, infer D> ? { dispatch: D } : never,
S
> {
return (createStore: StoreEnhancerStoreCreator<any>) => (
reducer,
...args: any[]
) => {
const store = createStore(reducer, ...args)
Expand Down