2739 Типу не хватает следующих свойств из типа - PullRequest
0 голосов
/ 05 марта 2020

Я пытаюсь написать службу API в приложении React, используя Ax ios и Typescript.

Это мой код:

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
    };
  };
};

И это мой полный ошибка:

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

1 Ответ

2 голосов
/ 05 марта 2020

Проблема в том, что вы неправильно определили interface Service.

Тип GetAll должен быть не Promise<AxiosResponse<T>>, а функцией, которая возвращает Promise<AxiosResponse<T>> в соответствии с вашей реализацией.

Итак, ваш Service интерфейс станет:

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