在构造函数 TS 中声明变量时的 Eslint 错误

问题描述 投票:0回答:2

这是我的 TypeScript 代码:

class Snake {
constructor(
    public x: number,       // error 'x' is defined but never used
    public y: number,       // error 'y' is defined but never used
    public size = 10,       // error 'size' is assigned a value but never used
    public color = (() => { 
        const tab = new Array(6).fill(0);
        return `#${tab.map(() => (Math.random() * 0xF << 0).toString(16)).join('')}`;
    })(),                   // error 'color' is assigned a value but never used
) { }

update() {
    const coef = (): number => {
        const nb = Math.round(Math.random());
        return nb === 0 ? -1 : 1;
    };
    this.x += Math.random() * 10 * coef();
    this.y += Math.random() * 10 * coef();
}
...

我安装了 eslint,它告诉我所有变量: '已定义但从未使用'并且在其下面我使用它们。我认为 eslint 不理解构造函数括号内的声明,但我不确定。我经常使用这种声明方法,所以如果我能解决这个问题,我会很高兴

{
"env": {
    "browser": true,
    "es2021": true
},
"extends": [
    "airbnb-base"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
    "ecmaVersion": "latest",
    "sourceType": "module"
},
"plugins": [
    "@typescript-eslint"
],
"rules": {
    "no-restricted-globals": "off",
    "consistent-return": "off", 
    "no-return-assign": "off", 
    "prefer-const": "off", 
    "no-param-reassign": "off", 
    "block-scoped-var": "off", 
    "no-use-before-define": "off", 
    "no-undef": "warn", 
    "no-unused-vars": "warn", 
    "no-plusplus": "off", 
    "no-var": "off", 
    "vars-on-top": "off", 
    "indent": [
        "error",
        4
    ], 
    "no-console": "off", 
    "no-mixed-spaces-and-tabs": "off", 
    "linebreak-style": "off", window
    "class-methods-use-this": "off", 
    "no-empty": "off", 
    "no-constant-condition": "off", 
    "nonblock-statement-body-position": [
        "error",
        "below"
    ], 
    "curly": "off", 
    "no-useless-constructor": "off",
    "no-empty-function": "off",
    "no-bitwise": "off"
}

这是我的 .eslint.json,错误来自“no-unused-vars”

javascript typescript eslint typescript-eslint
2个回答
0
投票

以下行有错误:

"linebreak-style": "off", window

window
这个词是额外的。

应该是

"linebreak-style": "off",

如果没有,从今天开始,eslint 配置将显示为无效。


0
投票

这是问题,因为 ESLint 规则被破坏(应该关闭):

'no-unused-vars': 'off',

所以应该使用规则形式

@typescript-eslint/eslint-plugin
插件:

'typeScriptEsLintPlugin/no-unused-vars': 'error',

就是这样。 以下是使用此设置的 ESLint 9 的示例平面配置:

import eslintJsPlugin from '@eslint/js';
import typeScriptEsLintPlugin from '@typescript-eslint/eslint-plugin';
import typescriptParser from '@typescript-eslint/parser';
import globals from 'globals';

export default [
  {
    languageOptions: {
      globals: {
        ...globals.node,
      },
    },
  },
  {
    files: ['**/*.ts'],
    ignores: ['package-lock.json'],
    languageOptions: {
      ecmaVersion: 'latest',
      sourceType: 'module',
      parser: typescriptParser,
      parserOptions: {
        warnOnUnsupportedTypeScriptVersion: false,
      },
    },
    plugins: {
      '@typescript-eslint': typeScriptEsLintPlugin,
      typeScriptEsLintPlugin,
    },
    rules: {
      ...eslintJsPlugin.configs.recommended.rules,
      'no-unused-vars': 'off',
      ...typeScriptEsLintPlugin.configs['eslint-recommended'].rules,
      'typeScriptEsLintPlugin/no-unused-vars': 'error',
    },
  },
];

© www.soinside.com 2019 - 2024. All rights reserved.