Recently delving into typescript, I decided to test the waters with nextjs
, rtk query
, and antd
. However, while attempting to access error
within useLoginMutation
using the property error.data.message
, it was flagged as type unknown
.
To tackle this issue, I implemented a type guard function.
import { FetchBaseQueryError } from "@reduxjs/toolkit/dist/query";
export default function isFetchBaseQueryError(
error: unknown
): error is FetchBaseQueryError {
return typeof error === "object" && error != null && "status" in error;
}
if (!isLoginLoading && error) {
if (isFetchBaseQueryError(error)) {
notification.error({
message: "Login Failed",
description: error.data.message, // this line is causing error
});
}
}