I am trying to create a errorMiddleware that will toast message whenever there is an error occuring from the api calls.
The action arguement is throwing a TS type error that says:
Property ‘error’ does not exist on type ‘{}’
import type { MiddlewareAPI, Middleware } from "@reduxjs/toolkit";
import { toast } from "react-toastify";
export const rtkQueryErrorLogger: Middleware =
(api: MiddlewareAPI) => (next) => (action) => {
if (action?.error) {
console.warn("We got a rejected action!");
toast.error(
"data" in action.error
? (action.error.data as { message: string }).message
: action.error.message
);
}
return next(action);
};