FMR
FMR

Reputation: 303

2739 Type is missing the following properties from type

I'm trying to write an API service in a React app, using Axios and Typescript.

This is my code:

interface Service<T> {
  GetAll?: Promise<AxiosResponse<T>>;
}
interface Example {
  id: Number;
}

const ApiService2 = () => {
  const Example = (): Service<Example> => {
    const GetAll = (): Promise<AxiosResponse<Example[]>> => {
      return axios.get<Example[]>("urlhere");
    };
    return {
      GetAll
    };
  };
};

And this is my complete error:

Type '() => Promise<AxiosResponse<Example[]>>' is missing the following properties from type 'Promise<AxiosResponse<Example>>': then, catch, [Symbol.toStringTag], finally  TS2739

Upvotes: 1

Views: 9716

Answers (1)

Hassan Naqvi
Hassan Naqvi

Reputation: 1061

The problem is, you defined you interface Service a bit wrong.

The type of GetAll should not be Promise<AxiosResponse<T>> but a function that returns Promise<AxiosResponse<T>> according to your implementation.

So your Service interface would become:

interface Service<T> {
  GetAll?: () => Promise<AxiosResponse<T>>;
}

Upvotes: 5

Related Questions