禁用 typescript-eslint 插件规则,使用内联注释。

huangapple go评论95阅读模式
英文:

Disable typescript-eslint plugin rule with inline comment

问题

I get an eslint warning that comes from the @typescript-eslint plugin.

> 'instance' is declared but its value is never read. ts(6133)

VSCode suggested the following line as a Quick Fix:

// eslint-disable-next-line @typescript-eslint/no-unused-vars

However, adding this line makes no difference, see below:

禁用 typescript-eslint 插件规则,使用内联注释。

Please note: I want to use an inline rule as shown here to disable the warning for one specific line only. I do not want to disable it for a whole file, nor disable it for the whole project.

PS: This is a fresh Vite project with TypeScript and React.

英文:

I get an eslint warning that comes from the @typescript-eslint plugin.

> 'instance' is declared but its value is never read. ts(6133)

VSCode suggested the following line as a Quick Fix:

// eslint-disable-next-line @typescript-eslint/no-unused-vars

However, adding this line makes no difference, see below:

禁用 typescript-eslint 插件规则,使用内联注释。

Please note: I want to use an inline rule as shown here to disable the warning for one specific line only. I do not want to disable it for a whole file, nor disable it for the whole project.

PS: This is a fresh Vite project with TypeScript and React.

答案1

得分: 1

我相信我找到了一个解决方案。

1.) tsconfig

tsconfig.json 中添加以下内容。这将阻止在使用 npm run build 构建项目时出现编译器错误。

在 VSCode 中,受影响的代码行仍会显示黄色下划线警告(如果您尚未采取其他措施来解决它们)。

 "compilerOptions": {
    /* Linting */
    "noUnusedLocals": false,
    "noUnusedParameters": false,
  },

2.) eslintrc

.eslintrc.cjs 中添加以下内容。这将确保未使用的变量被标记为警告。另一种选择是将它们标记为错误或完全忽略它们。请注意,我禁用了JS版本,但启用了TS版本。

同样,在 VSCode 中,受影响的代码行仍会显示黄色下划线警告(如果您尚未采取其他措施来解决它们)。

rules: {
    "no-unused-vars": "off",
    "@typescript-eslint/no-unused-vars": "warn",
  }

3.) eslint-disable

在您想要禁用警告的文件中,添加内联规则。请注意,如果您想具体指定,您必须添加规则的TS版本,例如:

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [instance, setInstance] = useState<SwiperClass | null>(null);

或者,您可以简单地使用 // eslint-disable-next-line 而不指定规则。

英文:

I believe I have found a solution.

1.) tsconfig

In tsconfig.json, add the following. This will stop the compiler errors from appearing when building the project with npm run build.

You will still get the yellow underline warnings for affected code lines in VSCode (if you haven't yet done anything else to address them).

 &quot;compilerOptions&quot;: {
    /* Linting */
    &quot;noUnusedLocals&quot;: false,
    &quot;noUnusedParameters&quot;: false,
  },

2.) eslintrc

In .eslintrc.cjs, add the following. This will ensure that unused variables get marked as warnings. Alternative options would be to mark them as errors or to ignore them completely. Note that I am disabling the JS version, but enabling the TS version.

Again, you will still get the yellow underline warnings for affected code lines in VSCode (if you haven't yet done anything else to address them).

rules: {
    &quot;no-unused-vars&quot;: &quot;off&quot;,
    &quot;@typescript-eslint/no-unused-vars&quot;: &quot;warn&quot;,
  }

3.) eslint-disable

In the file where you want to disable a warning, add an inline rule. Note that, if you want to be specific, you have to add the TS version of the rule, e.g.

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [instance, setInstance] = useState&lt;SwiperClass | null&gt;(null);

Alternatively, you can simply use // eslint-disable-next-line without specifying the rule.

答案2

得分: 0

我建议的是在您的eslintrc文件中添加以下规则:

rules: {
    ...,
    '@typescript-eslint/no-unused-vars': [
      'error',
      { argsIgnorePattern: '^_', varsIgnorePattern: '^_' },
    ],
}

有了这个规则,您可以通过以下方式消除错误:

const [_instance, setInstance] = useState(null)
英文:

What I would suggest is to add to your eslintrc file a following rule

rules: {
    ...,
    &#39;@typescript-eslint/no-unused-vars&#39;: [
      &#39;error&#39;,
      { argsIgnorePattern: &#39;^_&#39;, varsIgnorePattern: &#39;^_&#39; },
    ],
}

With this you can get the rid of the error this way :

const [_instance, setInstance] = useState(null)

答案3

得分: -1

你可以通过解构数组的第二个元素来避免这个错误,而不将第一个元素分配给一个变量:

const [, setInstance] = useState<SwiperClass | null>(null);


---

我不确定为什么你无法抑制这个错误,但可能你需要禁用不止一个规则。可能是内置的 [`no-unused-vars` 规则][1] 适用于这里?

// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars

[这里有一个答案][2] 关于禁用多个规则的语法。

  [1]: https://eslint.org/docs/latest/rules/no-unused-vars
  [2]: https://stackoverflow.com/a/56714489/10431574
英文:

You can avoid the error by destructuring the second element from the array without assigning the first element to a variable:

const [, setInstance] = useState&lt;SwiperClass | null&gt;(null);

I'm not sure exactly why you cannot suppress the error, but it's possible that you need to disable more than one rule. Possibly the built-in no-unused-vars rule applies here?

// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars

Here's an answer about the syntax for disabling multiple rules.

huangapple
  • 本文由 发表于 2023年6月27日 19:30:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76564413.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定