Skip to content
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

Argument of type 'unknown' is not assignable to parameter of type 'Action<string>'. #3895

Closed
andreisocaciu opened this issue Nov 20, 2023 · 2 comments

Comments

@andreisocaciu
Copy link

When trying to match an action in a middleware, I get this Typescript error: Argument of type 'unknown' is not assignable to parameter of type 'Action<string>'. typescript(2345)

Version: @redux/toolkit@2.0.0-rc.0

Codesandbox link: https://codesandbox.io/p/sandbox/kind-fog-7nxp6k?file=%2Fshared%2Fcounter.middleware.ts%3A5%2C31-5%2C37

@EskiMojo14
Copy link
Collaborator

yep - you need to check it's an action first. Redux now exports an isAction type predicate that can do this for you:

import { isAction, Middleware } from "@reduxjs/toolkit";
import { incrementByAmount } from "./counter.slice";

export const counterMiddleware: Middleware = (store) => (next) => (action) => {
  if (isAction(action) && incrementByAmount.match(action)) {
    console.log("action dispatched", action);
  }
  return next(action);
};

@EskiMojo14
Copy link
Collaborator

EskiMojo14 commented Nov 20, 2023

fyi we're now planning to move the isAction call inside .match so you won't have to do this yourself (#3897) - we're also gonna move isAction to the core package (reduxjs/redux#4620) and re-export it from RTK, since it's likely useful for people using handwritten redux patterns too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants