1

Is there any way to specify the type for error returned from server for a specific endpoint and for api requests in general, so that I can do

const [login, {error}] = useLoginMutation();

alert(error.data.message)//message is in my custom type

I am using the default fetchBaseQuery. I saw this https://stackoverflow.com/a/70118482/5599387 and https://github.com/rtk-incubator/rtk-query/issues/86#issuecomment-738845312, but I get

Type 'FetchBaseQueryError' is not comparable to type 'CustomError'.
    Property 'message' is missing in type '{ status: "CUSTOM_ERROR"; data?: unknown; error: string; }' but required in type 'CustomError'
    export const apiSlice = createApi({
      reducerPath: "api",
      baseQuery: fetchBaseQuery({
        baseUrl: "https://...",
        prepareHeaders: (headers) => {
          if (localStorage.getItem("token"))
            headers.set(
              "Authorization",
              `Bearer ${localStorage.getItem("token") || ""}`,
            );
    
          return headers;
        },
      }) as BaseQueryFn<string | FetchArgs, unknown, CustomError, {}>,
      endpoints: () => ({}),
    });

    type CustomError = {
      message: string;
      status: number;
    }

I could do like this answer https://stackoverflow.com/a/70025645/5599387 but how do I add custom types like message to it?

How do I specify error type for a mutation or at least for all requests in general?

Abhi
  • 1,512
  • 2
  • 22
  • 46
  • Here is a solution: [https://stackoverflow.com/a/75943207/17064354](https://stackoverflow.com/a/75943207/17064354) Maybe this answer can help!!! – Sabbir Zzaman Apr 05 '23 at 19:37
  • Here is a solution: [https://stackoverflow.com/a/75943207/17064354](https://stackoverflow.com/a/75943207/17064354) Maybe this answer can help!!! – Sabbir Zzaman Apr 05 '23 at 19:42

0 Answers0