关于reactjs:Typescript eslint禁用no-unused-vars

Typescript eslint disable no-unused-vars

我正在研究Typescript React项目,通常将占位符变量放入代码中,以便对所有内容进行布局,直到实现所有内容为止。这会导致一堆eslint no-unused-vars错误,并使发现真正的错误变得很困难。

如何在我准备就绪之前如何全局禁用它?我使用create-react-app my-app --typescript并不想弹出项目,但不确定如何禁用此警告。

我注意到package.json中有一个eslintConfig部分,因此我尝试关闭

1
2
3
4
5
6
"eslintConfig": {
   "extends":"react-app",
   "rules": {
     "no-unused-vars":"off"
    }
 },

Update


(已删除<

我更新了package.json并不断收到错误。

1
2
3
4
5
6
"eslintConfig": {
   "extends":"react-app",
   "rules": {
     "@typescript-eslint/no-unused-vars":"off"
    }
  },

我尝试将规则移至项目根目录中的.eslintrc.json文件中,并且仍然

似乎唯一起作用的是将// eslint-disable-line @typescript-eslint/no-unused-vars放在变量后面。


我认为有些混乱。

问题和唯一答案都建议在tsconfig.json中放置一个规则部分。这是我从未听说过的东西,在文档或官方架构中也没有提及.https://www.typescriptlang.org/docs/handbook/tsconfig-json.html

也,问题是关于在eslint中禁用规则,因此规则更改应放在.eslintrc.json文件(或问题中考虑的package.json中的eslintConfig部分)中。

但这是打字稿和typescript-eslint,因此规则应为:

"@typescript-eslint/no-unused-vars" :"off"

,答案中提出的hacky解决方案需要更改为与某人链接(请注意,答案是针对js的eslint,而不是ts)。

/ * eslint-disable @ typescript-eslint / no-unused-vars * /

这些解决方案对我有用[email protected]@typescript -eslint/[email protected]@ typescript-eslint / parser @ 2.3.2


由于您使用了create-react-app my-app --typescript,因此在my-app/

中应该已经为您创建了tsconfig.json。在您的tsconfig.json中,您可以为打字稿编译器添加规则。

1
2
3
4
5
6
7
8
9
10
11
12
{
 "extends": [...],
 "compilerOptions": {
    ...
  },
 "include": [...],
 "rules": {
    ...
   "no-unused-vars":"off"
    ...
  }
}


我有类似的问题。这是我的解决方案:

  • .eslintrc[.json]重命名为.eslintrc.js
  • 在先前Json对象的前面添加module.exports = 。因此.eslintrc.js看起来像这样:
  • 1
    2
    3
    4
    5
    module.exports = {
        ...
       "rules": {...},
        ...
    }
  • 根据您当前的环境设置"@typescript-eslint/no-unused-vars"规则。这可以通过检查process.env.NODE_ENV变量来实现。我的选择是在生产中出现错误,并在其他情况下被警告。该代码段如下所示:
  • 1
    2
    3
    4
    5
    6
    7
    8
    module.exports = {
        ...
       "rules": {
             ...
            "@typescript-eslint/no-unused-vars": process.env.NODE_ENV ==="production" ?"error" :"warn"
        },
        ...
    }

    在eslintrc.json内添加" rules ",您只需将no-unused-vars写入0。

    " rules ":{" no-unused-vars " :0}