Skip to content

Commit

Permalink
add DeclaredMiddlewareType helper type to specify middleware signatur…
Browse files Browse the repository at this point in the history
…e on extended function objects
  • Loading branch information
phryneas committed May 10, 2020
1 parent 0d58077 commit 6330ef9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion etc/redux-toolkit.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export function createActionListenerMiddleware<S, D extends Dispatch<AnyAction>
<C_1 extends TypedActionCreator<any>>(actionCreator: C_1, listener: ActionListener<ReturnType<C_1>, S, D, any>): boolean;
(type: string, listener: ActionListener<AnyAction, S, D, any>): boolean;
};
};
} & WithMiddlewareType<Middleware<(action: Action<"actionListenerMiddleware/add">) => () => void, S, D>>;

// @public (undocumented)
export function createAsyncThunk<Returned, ThunkArg = void, ThunkApiConfig extends AsyncThunkConfig = {}>(typePrefix: string, payloadCreator: (arg: ThunkArg, thunkAPI: GetThunkAPI<ThunkApiConfig>) => Promise<Returned | RejectWithValue<GetRejectValue<ThunkApiConfig>>> | Returned | RejectWithValue<GetRejectValue<ThunkApiConfig>>, options?: AsyncThunkOptions<ThunkArg, ThunkApiConfig>): IsAny<ThunkArg, (arg: ThunkArg) => AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig>, unknown extends ThunkArg ? (arg: ThunkArg) => AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig> : [ThunkArg] extends [void] | [undefined] ? () => AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig> : [void] extends [ThunkArg] ? (arg?: ThunkArg | undefined) => AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig> : [undefined] extends [ThunkArg] ? (arg?: ThunkArg | undefined) => AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig> : (arg: ThunkArg) => AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig>> & {
Expand Down
1 change: 0 additions & 1 deletion src/createActionListenerMiddleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ describe('createActionListenerMiddleware', () => {
const unsubscribe = store.dispatch(addListenerAction(testAction1, listener))

store.dispatch(testAction1('a'))
// @ts-ignore TODO types
unsubscribe()
store.dispatch(testAction2('b'))
store.dispatch(testAction1('c'))
Expand Down
7 changes: 6 additions & 1 deletion src/createActionListenerMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Middleware, Dispatch, AnyAction, MiddlewareAPI, Action } from 'redux'
import { TypedActionCreator } from './mapBuilders'
import { createAction, BaseActionCreator } from './createAction'
import { WithMiddlewareType } from './tsHelpers'

export type When = 'before' | 'after' | undefined
type WhenFromOptions<
Expand Down Expand Up @@ -349,5 +350,9 @@ export function createActionListenerMiddleware<
}
}

return Object.assign(middleware, { addListener, removeListener })
return Object.assign(
middleware,
{ addListener, removeListener },
{} as WithMiddlewareType<typeof middleware>
)
}
19 changes: 18 additions & 1 deletion src/tsHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,31 @@ export type IsUnknownOrNonInferrable<T, True, False> = AtLeastTS35<
IsEmptyObj<T, True, IsUnknown<T, True, False>>
>

const declaredMiddlewareType: unique symbol = undefined as any
export type WithMiddlewareType<T extends Middleware<any, any, any>> = {
[declaredMiddlewareType]: T
}

/**
* Combines all dispatch signatures of all middlewares in the array `M` into
* one intersected dispatch signature.
*/
export type DispatchForMiddlewares<M> = M extends ReadonlyArray<any>
? UnionToIntersection<
M[number] extends infer MiddlewareValues
? MiddlewareValues extends Middleware<infer DispatchExt, any, any>
? MiddlewareValues extends WithMiddlewareType<
infer DeclaredMiddlewareType
>
? DeclaredMiddlewareType extends Middleware<
infer DispatchExt,
any,
any
>
? DispatchExt extends Function
? DispatchExt
: never
: never
: MiddlewareValues extends Middleware<infer DispatchExt, any, any>
? DispatchExt extends Function
? DispatchExt
: never
Expand Down

0 comments on commit 6330ef9

Please sign in to comment.