忽略TS6133:"(导入)已声明但从未使用过"?

jdm*_*jdm 18 typescript

在处理TypeScript项目时,我注释掉了一行,并得到了错误:

编译失败

./src/App.tsx
(4,8): error TS6133: 'axios' is declared but never used.
Run Code Online (Sandbox Code Playgroud)

在构建期间发生此错误,无法解除.

错误是对的,我正在导入axios,但我想暂时注释掉这个调用axios.get.我很欣赏这个错误,因为它保持了我的进口清洁,但在早期开发过程中非常具有破坏性.

有什么方法可以禁用或忽略该警告?

Sar*_*ana 24

你可能已经noUnusedLocals打开了编译器选项tsconfig.json.在开发过程中关闭它.

  • 可以设置为警告吗? (4认同)
  • 这似乎不起作用。根据文档,默认情况下“noUsedLocals”为 false。但是,省略它仍然会显示错误,并且即使将其设置为 false,错误仍然存​​在。TS服务器和vscode重启后。也没有其他 tsconfig 文件。 (4认同)
  • 就我而言,我也忘记了重新启动`npm run start`才能使更改生效。 (2认同)

xia*_*ia2 17

在 tsconfig.json 中

{
  "compilerOptions": {
    ...
    "noUnusedLocals": false,   // just set this attribute false
  }
}
Run Code Online (Sandbox Code Playgroud)

将会完成。

如需更多提示:

在 xxx.ts 文件中

//@ts-nocheck 
when on the top of the file,it will not check the below.

//@ts-ignore
when use it,it will not check the next line
Run Code Online (Sandbox Code Playgroud)


小智 6

我在我的 React 应用程序中遇到了同样的问题。除了更改 "noUsedLocals": false中的属性外tsconfig.json,您还需要调整"noUnusedParameters": false. 前者仅适用于局部变量,如果通过函数传递未使用的参数,则后者也需要更改为 false。

总之,您必须执行以下操作:

{
  "compilerOptions": {
     "noUnusedLocals": false,
     "noUnusedParameters": false,
 }
}
Run Code Online (Sandbox Code Playgroud)