【前端问题精选】typescript写fetch请求时候报错

如题,fetch请求直接报参数类型错误

  • 同样的代码在windows下的vscode中使用正常
  • 在Mac OS X下使用时报错

代码

function test(params?:any) {

fetch('localhost:8003',{

headers:{

}

})

}

报错信息

ERROR in [at-loader] ./src/index.tsx:75:28

TS2345: Argument of type '{ headers: {}; }' is not assignable to parameter of type 'RequestInit'.

Types of property 'headers' are incompatible.

Type '{}' is not assignable to type 'Headers | string[][]'.

Type '{}' is not assignable to type 'string[][]'.

Property '[Symbol.unscopables]' is missing in type '{}'.

图片描述

在使用ant design时候也出现这种类型判断错误导致编译失败的情况

回答:

  • typescript 版本不一致导致的
  • 全局安装typescript的话还是没法儿做到版本统一 所以以后将typescript安装到当前目录下 package.json记录版本信息

以上是 【前端问题精选】typescript写fetch请求时候报错 的全部内容, 来源链接: utcz.com/a/133111.html

回到顶部