Skip to content

Commit

Permalink
fix type tests - make next unknown function
Browse files Browse the repository at this point in the history
  • Loading branch information
ben.durrant committed Apr 11, 2023
1 parent 3b79b52 commit 3f9631a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/types/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ export interface Middleware<
D extends Dispatch = Dispatch
> {
(api: MiddlewareAPI<D, S>): (
next: D
) => (action: unknown) => any
next: (action: unknown) => unknown
) => (action: unknown) => unknown
}
32 changes: 14 additions & 18 deletions test/typescript/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
*/
function logger() {
const loggerMiddleware: Middleware =
({ getState }: MiddlewareAPI) =>
(next: Dispatch) =>
({ getState }) =>
next =>
action => {
console.log('will dispatch', action)

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

function promise() {
const promiseMiddleware: Middleware<PromiseDispatch> =
({ dispatch }: MiddlewareAPI) =>
({ dispatch }) =>
next =>
<T extends Action>(action: AnyAction | Promise<T>) => {
action => {
if (action instanceof Promise) {
action.then(dispatch)
return action
Expand Down Expand Up @@ -72,13 +72,10 @@ function thunk<S, DispatchExt>() {
ThunkDispatch<S, DispatchExt>,
S,
Dispatch & ThunkDispatch<S>
> =
api =>
(next: Dispatch) =>
<R>(action: AnyAction | Thunk<R, any>) =>
typeof action === 'function'
? action(api.dispatch, api.getState)
: next(action)
> = api => next => action =>
typeof action === 'function'
? action(api.dispatch, api.getState)
: next(action)

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

const customMiddleware: Middleware<{}, State> =
api => (next: Dispatch) => action => {
api.getState().field
// @ts-expect-error
api.getState().wrongField
const customMiddleware: Middleware<{}, State> = api => next => action => {
api.getState().field
// @ts-expect-error
api.getState().wrongField

return next(action)
}
return next(action)
}

return customMiddleware
}
Expand Down

0 comments on commit 3f9631a

Please sign in to comment.