Search code examples
reactjstypescripterror-handlingtry-catch

i want to remove any from this catch block , below is my code


const apiCallingFunction = async (data: any) =\> {
  try {
    let response = await axiosPost("/api", { ...data });
    return response;
  } catch (err: any) {
    return err.response;
  }
};

I want to remove any from the catch block.


Solution

  • import { AxiosError } from "axios";
    
    const apiCallingFunction = async (data: any) => {
      try {
      let response = await axiosPost("/api", { ...data });
      return response;
      } catch (err: unknown) {
       return (err as AxiosError).response;
      }
     };
    

    Axios Has Inbuilt Type for error Known As AxiosError