小编Her*_*cke的帖子

如何修复 Typescript 编译错误 ts2345“缺少类型‘响应’......来自类型‘响应’:重定向、拖车、formData!”

我试图node-fetch在我的打字稿项目中提出请求,但我不明白如何修复编译错误或它实际上试图告诉我什么。

我已将所有包(包括全局打字稿包)更新到最新版本。

我创建了一个隔离错误的要点:https : //gist.github.com/HerrZatacke/ae90f608e042864b6e00e9c73a950602

这个(非常短的)脚本能够重现编译错误:

import fetch from 'node-fetch';

const toJson = (response: Response):PromiseLike<object> => (
  response.json()
);

const makeSomeRequest = (): Promise<object> => {
  return fetch('https://some-api.com/')
    .then(toJson)
};

makeSomeRequest();
Run Code Online (Sandbox Code Playgroud)

使用的安装版本是

@types/node-fetch 2.3.7
node-fetch2.6.0
typescript 3.5.2
Run Code Online (Sandbox Code Playgroud)

实际错误是

example.ts:9:11 - error TS2345: Argument of type '(response: Response) => PromiseLike<object>' is not assignable to parameter of type '(value: Response) => object | PromiseLike<object>'.
  Types of parameters 'response' and 'value' are incompatible.
    Type 'Response' is missing the following properties …
Run Code Online (Sandbox Code Playgroud)

node.js promise typescript node-fetch

2
推荐指数
1
解决办法
2315
查看次数

标签 统计

node-fetch ×1

node.js ×1

promise ×1

typescript ×1