小编Leo*_*sse的帖子

使用 node-fetch 时如何使请求正文类型与 RequestInit 或 BodyInit 兼容?

我开始在我的 nodejs 项目中使用 Typescript。为了访问一些外部 API,我使用 node-fetch 来发出请求。在设置 PUT 请求时,会弹出一个错误,说给定的主体不可分配给 type RequestInit

错误:

Error:(176, 28) TS2345:Argument of type '{ headers: Headers; method: string; body: MyClass; }' is not assignable to parameter of type 'RequestInit'.
  Types of property 'body' are incompatible.
    Type 'MyClass' is not assignable to type 'BodyInit'.
      Type 'MyClass' is not assignable to type 'ReadableStream'.
        Property 'readable' is missing in type 'MyClass'.
Run Code Online (Sandbox Code Playgroud)

我的课:

class MyClass {
  configId: string;
  adapterType: string;
  address: string;

  constructor(configId: string, adapterType: string, address: string) { …
Run Code Online (Sandbox Code Playgroud)

node.js typescript node-fetch

13
推荐指数
3
解决办法
1万
查看次数

标签 统计

node-fetch ×1

node.js ×1

typescript ×1