tslint如何禁用错误"someVariable已声明,但其值永远不会被读取"

Pat*_*atS 40 tslint

我正在使用tslint,并得到了错误.

'myVariable' is declared but its value is never read.
Run Code Online (Sandbox Code Playgroud)

我去了记录规则https://palantir.github.io/tslint/rules/的网站,并搜索了字符串,is declared but its value is never read但没有找到该文本.虽然我可以并且确实寻找可能与此错误相关的设置,但它不应该是猜谜游戏.

抑制/停止此错误需要什么配置更改?

同样重要的是,当我在tslint中收到"发生这种情况"的错误时,如何找到用于配置或更改如何处理该错误的tslint行为的设置?

我也在网站上搜索过(我使用的谷歌搜索是)

site:palantir.github.io  is declared but its value is never read 
Run Code Online (Sandbox Code Playgroud)

但直接命中没有出现,所以答案可能在palantir.github.io网站上,但我还没有(还)找到它.

其他人如何找到更改的tslint变量/配置设置以抑制特定错误?

请不要建议我注释掉导致问题的代码.我正在寻找一个更普遍的问题以及具体问题的答案.谢谢.

Rac*_*wat 34

任何以_开头的参数名称都免于检查.使用_myVariable而不是myvariable删除此警告.

  • 它不会自动发生,您必须指定忽略模式.`"no-unused-variable":[true,{"ignore-pattern":"^ _"}]`但是,这是一个很好的解决方案. (8认同)
  • 作为更新,自TypeScript 2.9开始不赞成使用“ no-unused-variable”。https://github.com/palantir/tslint/issues/4046 (4认同)
  • 值得注意的是,由于 tslint 已被弃用,您应该使用 eslint 和 typescript-eslint 代替。使用 _ 不会自动绕过 no-unused-vars 规则,但需要像 Rachit 所说的配置。https://eslint.org/docs/rules/no-unused-vars。使用 _ 仍然会自动绕过 TS 编译器。 (4认同)
  • 我不必指定该模式,立即为我工作 (2认同)

Rog*_*ger 25

拳头问题:

编辑文件:tsconfig.json,添加/修改密钥" noUnusedLocals ":false.

您需要重新启动服务器.

第二个问题:

如果是一个tslint错误; VS代码在错误消息中显示已应用的规则.

Identifier 'doc' is never reassigned; use 'const' instead of 'let'. (prefer-const)
Run Code Online (Sandbox Code Playgroud)

在这种情况下,prefer-const规则.

  • 或者你显然可以[声明你只在模板`protected`中使用的变量](https://github.com/Microsoft/TypeScript/issues/19700#issuecomment-348950146),而不是关闭检查任何未使用的本地变量。 (2认同)

edw*_*win 24

在导致错误的行之前添加此行:

  /* tslint:disable:no-unused-variable */
Run Code Online (Sandbox Code Playgroud)

您将不再收到tslint错误消息.

这比在tslint.conf中为整个代码库关闭错误更好的解决方案,因为它不会捕获真正未使用的变量.


Mas*_*iri 22

首先noUnusedLocals在 tsconfig.json 中关闭:

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

然后修复 eslint 规则.eslintrc.js

module.exports = {
  rules: {
    'no-unused-vars': ['warn', { 'varsIgnorePattern': '^_' }],
    '@typescript-eslint/no-unused-vars': ['warn', { 'varsIgnorePattern': '^_' }],
  },
};
Run Code Online (Sandbox Code Playgroud)

并且如果使用 @typescript-eslint/naming-convention规则应该添加leadingUnderscore: 'allow',例如,如果您使用的是Airbnb配置:

'@typescript-eslint/naming-convention': [
  'error',
  {
    selector: 'variable',
    format: ['camelCase', 'PascalCase', 'UPPER_CASE'],
    leadingUnderscore: 'allow',
  },
  {
    selector: 'function',
    format: ['camelCase', 'PascalCase'],
  },
  {
    selector: 'typeLike',
    format: ['PascalCase'],
  },
],
Run Code Online (Sandbox Code Playgroud)

注意:您应该eslint手动将 package.json 中的所有相关包更新到最新版本。


ana*_*han 18

使用 dev.tsconfig.json 扩展 tsconfig.json

并运行命令 tsc -p ./dev.tsconfig.json

这将禁用开发中未使用的变量和未使用的参数

dev.tsconfig.json 内部:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "noUnusedLocals": false,
    "noUnusedParameters": false,
   }
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,是 noUnusedParameters 搞砸了我 (2认同)

Wan*_*ker 5

我正在使用typescript": "2.9.1"tslint": "^5.10.0.

我收到了大量的错误,例如

Property 'logger' is declared but its value is never read.
Run Code Online (Sandbox Code Playgroud)

另外,我观察到运行时收到警告ng-lint

$> ng lint
no-unused-variable is deprecated. Since TypeScript 2.9. Please use the built-in compiler checks instead.
Run Code Online (Sandbox Code Playgroud)

所以,我删除了no-unused-variable规则tslint.json- 这似乎解决了我的问题。