-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Middleware action
is typed as any
#4518
Comments
That's because as far as the middleware is concerned, that export interface Middleware<
DispatchExt = {},
S = any,
D extends Dispatch = Dispatch
> {
(api: MiddlewareAPI<D, S>): (
next: Dispatch<AnyAction>
) => (action: any) => any
} It's up to you to do appropriate type guard checks if you need to narrow it down. |
if you want this to be more explicit, you can force action to be unknown: export const loggerMiddleware: AppMiddleware =
(storeApi) => (next) => (action: unknown) => { this would force you to check what the action is before you can do anything with it. |
Is there a way to do this without using For example, is there a known way to pull the action type for all actions? const actions = {
...deviceSlice.actions,
...authSlice.actions,
};
type AppAction = typeof actions; |
by doing so, you would be lying to Typescript: Do not create union types with Redux Action Types. It's most likely an antipattern. if you want to check whether an action is a known one, you can use the action creator's match method and/or RTK's matching utilities. the simple fact is that you do not know what is being dispatched - in a middleware, it could be anything (thunks for example are functions and would not have a .type property at all) |
action
is typed as any
For context, I was trying to migrate an existing redux library to toolkit. In my existing codebase, I had full type-safety around actions, dispatches, and middleware. But this relied on a I guess my next best bet is to type it as |
Yep. Type Per that article, we really don't find it useful to limit what can be passed into |
yeah, so for example your reduxWhitelist check could instead look like: export type AppMiddleware<DispatchExt = {}> = Middleware<DispatchExt, AppState, AppDispatch>;
const isWhitelistedAction = isAnyOf(chatCreated, chatDeleted);
export const syncMiddleware: AppMiddleware = (mwApi) => (next) => (action: unknown) => {
if (isWhitelistedAction(action)) {
// inside this block, action is now a union of all the actions whitelisted
syncQueue.set([action.type]);
}
return next(action);
} |
I would also very much like it to default to |
I am also interested in defaulting those to |
Although after discussing it some more, I think they're always contravariant, but it will still take more work to avoid |
I experimented a bit with replacing
When it comes to the tests, it broke two tests:
|
yeah, you're right - i was running into a bunch of headaches with Action not being assignable to UnknownAction, but changing Action to a type fixes it (technically this would be a breaking change in that you couldn't use declaration merging any more, but I don't think we'd want anyone merging the basic Action type anyway) |
The documentation is missing typing on
action
. https://redux.js.org/usage/usage-with-typescript#type-checking-middlewareThe text was updated successfully, but these errors were encountered: