Skip to content

Commit d8926bc

Browse files
authored
Merge pull request #4519 from EskiMojo14/mw-action-unknown
fixes undefined
2 parents 6add8a2 + 0693e5e commit d8926bc

File tree

4 files changed

+31
-40
lines changed

4 files changed

+31
-40
lines changed

src/types/middleware.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ export interface MiddlewareAPI<D extends Dispatch = Dispatch, S = any> {
2020
* installed.
2121
*/
2222
export interface Middleware<
23-
_DispatchExt = {}, // TODO: remove unused component (breaking change)
23+
_DispatchExt = {}, // TODO: see if this can be used in type definition somehow (can't be removed, as is used to get final dispatch type)
2424
S = any,
2525
D extends Dispatch = Dispatch
2626
> {
2727
(api: MiddlewareAPI<D, S>): (
28-
next: D
29-
) => (action: D extends Dispatch<infer A> ? A : never) => any
28+
next: (action: unknown) => unknown
29+
) => (action: unknown) => unknown
3030
}

test/applyMiddleware.spec.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ describe('applyMiddleware', () => {
2828
})
2929

3030
it('wraps dispatch method with middleware once', () => {
31-
function test(spyOnMethods: any) {
32-
return (methods: any) => {
31+
function test(spyOnMethods: any): Middleware {
32+
return methods => {
3333
spyOnMethods(methods)
34-
return (next: Dispatch) => (action: Action) => next(action)
34+
return next => action => next(action)
3535
}
3636
}
3737

@@ -53,8 +53,8 @@ describe('applyMiddleware', () => {
5353
})
5454

5555
it('passes recursive dispatches through the middleware chain', () => {
56-
function test(spyOnMethods: any) {
57-
return () => (next: Dispatch) => (action: Action) => {
56+
function test(spyOnMethods: any): Middleware {
57+
return () => next => action => {
5858
spyOnMethods(action)
5959
return next(action)
6060
}
@@ -146,8 +146,7 @@ describe('applyMiddleware', () => {
146146
}
147147

148148
function dummyMiddleware({ dispatch }: MiddlewareAPI) {
149-
return (_next: Dispatch) => (action: Action) =>
150-
dispatch(action, testCallArgs)
149+
return (_next: unknown) => (action: any) => dispatch(action, testCallArgs)
151150
}
152151

153152
const store = createStore(

test/helpers/middleware.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
import { MiddlewareAPI, Dispatch, AnyAction } from 'redux'
1+
import { Dispatch, Middleware } from 'redux'
22

3-
type ThunkAction<T extends any = any> = T extends AnyAction
4-
? AnyAction
5-
: T extends Function
6-
? T
7-
: never
8-
9-
export function thunk({ dispatch, getState }: MiddlewareAPI) {
10-
return (next: Dispatch) =>
11-
<_>(action: ThunkAction) =>
12-
typeof action === 'function' ? action(dispatch, getState) : next(action)
13-
}
3+
export const thunk: Middleware<{
4+
<R>(thunk: (dispatch: Dispatch, getState: () => any) => R): R
5+
}> =
6+
({ dispatch, getState }) =>
7+
next =>
8+
action =>
9+
typeof action === 'function' ? action(dispatch, getState) : next(action)

test/typescript/middleware.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import {
1515
*/
1616
function logger() {
1717
const loggerMiddleware: Middleware =
18-
({ getState }: MiddlewareAPI) =>
19-
(next: Dispatch) =>
18+
({ getState }) =>
19+
next =>
2020
action => {
2121
console.log('will dispatch', action)
2222

@@ -41,9 +41,9 @@ type PromiseDispatch = <T extends Action>(promise: Promise<T>) => Promise<T>
4141

4242
function promise() {
4343
const promiseMiddleware: Middleware<PromiseDispatch> =
44-
({ dispatch }: MiddlewareAPI) =>
44+
({ dispatch }) =>
4545
next =>
46-
<T extends Action>(action: AnyAction | Promise<T>) => {
46+
action => {
4747
if (action instanceof Promise) {
4848
action.then(dispatch)
4949
return action
@@ -72,13 +72,10 @@ function thunk<S, DispatchExt>() {
7272
ThunkDispatch<S, DispatchExt>,
7373
S,
7474
Dispatch & ThunkDispatch<S>
75-
> =
76-
api =>
77-
(next: Dispatch) =>
78-
<R>(action: AnyAction | Thunk<R, any>) =>
79-
typeof action === 'function'
80-
? action(api.dispatch, api.getState)
81-
: next(action)
75+
> = api => next => action =>
76+
typeof action === 'function'
77+
? action(api.dispatch, api.getState)
78+
: next(action)
8279

8380
return thunkMiddleware
8481
}
@@ -89,14 +86,13 @@ function thunk<S, DispatchExt>() {
8986
function customState() {
9087
type State = { field: 'string' }
9188

92-
const customMiddleware: Middleware<{}, State> =
93-
api => (next: Dispatch) => action => {
94-
api.getState().field
95-
// @ts-expect-error
96-
api.getState().wrongField
89+
const customMiddleware: Middleware<{}, State> = api => next => action => {
90+
api.getState().field
91+
// @ts-expect-error
92+
api.getState().wrongField
9793

98-
return next(action)
99-
}
94+
return next(action)
95+
}
10096

10197
return customMiddleware
10298
}

0 commit comments

Comments
 (0)