Как определить макет моего топора ios, используя TypeScript - PullRequest
0 голосов
/ 21 апреля 2020

Я пытаюсь издеваться axios.get, используя шутку. Я использую TypeScript, но почему-то не могу найти, как удовлетворить определения типов TypeScript.

const axiosOK = {
  status: 200,
  statusText: 'OK',
  headers: {},
  config: {},
};
const targets: AxiosResponse<Target[]> = {
  ...axiosOK,
  data: [
    {
      id: '1904fbed23b1a57f7a079bce5e43bd960813bbbe',
      gun: 'localhost:5000/react/test',
      role: 'targets',
    },
  ],
};

beforeEach(() => {
  axios.get = jest.fn(() => Promise.resolve(targets));
});
afterEach(cleanup);

Это приводит к следующей ошибке в строке axios.get = ......

Type 'Mock<Promise<AxiosResponse<Target[]>>, []>' is not assignable to type '<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig | undefined) => Promise<R>'.
  Type 'Promise<AxiosResponse<Target[]>>' is not assignable to type 'Promise<R>'.
    Type 'AxiosResponse<Target[]>' is not assignable to type 'R'.
      'AxiosResponse<Target[]>' is assignable to the constraint of type 'R', but 'R' could be instantiated with a different subtype of constraint '{}'.ts(2322)

Я довольно новичок в машинописи, поэтому, вероятно, я просто борюсь с синтаксисом. Я попытался определить тип для get, чтобы выровнять T с R, но, похоже, это только ухудшает ситуацию.

например,

axios.get = jest.fn<Target[], AxiosResponse<Target[]>>(() => Promise.resolve(targets));

дает

Type 'AxiosResponse<Target[]>' does not satisfy the constraint 'any[]'.
  Type 'AxiosResponse<Target[]>' is missing the following properties from type 'any[]': length, pop, push, concat, and 28 more.ts(2344)
...