Eslint规则@typescript-eslint/命名约定与@typescript-eslint/no未使用的变量冲突

aamkag61  于 2023-02-17  发布在  TypeScript
关注(0)|答案(1)|浏览(446)

我正在为一个打字项目设置eslint。
这是在重构情况下未使用变量的规则

"@typescript-eslint/no-unused-vars": [
  "error",
  {
    "argsIgnorePattern": "^_",
    "ignoreRestSiblings": true,
    "destructuredArrayIgnorePattern": "^_"
  }
],

//ex:
// error lint
const [a, b] = ['a', 'b']
console.log(b)

// ok
const [_a, b] = ['a', 'b']
console.log(b)

但是@typescript-eslint/naming-convention规则触发了一个错误。我没有在规则中指定它,但我认为它是通过插件“@typescript-eslint”添加的,我不想删除它。
我怎样才能使一条规则不干扰另一条规则呢?或者怎样使用这两条规则

pcww981p

pcww981p1#

@typescript-eslint/naming-conventiontypescript-eslint自带的任何配置中通常都是默认不启用的。如果在配置中设置了它,则必须扩展其他一些启用它的配置。
对于您的特定示例,您可以跳过数组解构模式中的特定元素,只需在元素所在的位置放置一个逗号即可。请参见Destructuring assignment中的以下示例:

function f() {
  return [1, 2, 3];
}

const [a, , b] = f();
console.log(a); // 1
console.log(b); // 3

const [c] = f();
console.log(c); // 1

或者在您的情况下:

const [, b] = ['a', 'b']

然而,更一般地说,如果你确实想在一个未使用的变量前面加上下划线,@typescript-eslint/naming-convention规则中有一个选项可以实现。

"@typescript-eslint/naming-convention": [
  "error",
  {
    "selector": "parameter",
    "modifiers": ["unused"],
    "format": ["strictCamelCase"],
    "leadingUnderscore": "require"
  },
]

根据自己的需要进行调整。您可以将其用于变量以外的其他类型,或者不必使用"严格"的驼峰式大小写(与camelCase相同,但不允许连续大写[即myId有效,但myID无效]),或者您可以允许下划线,但不一定是必需的。

相关问题