ESlint 在 class 中给出了关于索引器签名的错误

ESlint gives error about the indexer signature in class

我有一个带有索引器签名的简单 class:

class Test {
  [key: string]: any
}

它可以通过 tsc 编译,但是 eslint 会报错:

'key' is not defined. eslint no-undef
(parameter) key: string

这是.eslintrc.json.

中的配置
{
    "env": {
        "browser": true,
        "node": true,
        "es2021": true
    },
    "extends": [
        "eslint:recommended",
        "plugin:react/recommended",
        "google"
    ],
    "parser": "@typescript-eslint/parser",
    "parserOptions": {
        "ecmaFeatures": {
            "jsx": true
        },
        "ecmaVersion": 12,
        "sourceType": "module"
    },
    "plugins": [
        "react",
        "@typescript-eslint"
    ],
    "rules": {
        "object-curly-spacing": "off",
        "camelcase": "off",
        "new-cap": "off",
        "react/react-in-jsx-scope": "off",
        "no-invalid-this": "off",
        "no-unused-vars": [
            "warn",
            {
                "ignoreRestSiblings": true
            }
        ]
    },
    "globals": {
        "JSX": "readonly"
    }
}

我怎样才能摆脱这个 eslint 错误?

两个选项

  1. 去掉eslint:recommended扩展,google扩展就够了。
  2. 在规则中设置'no-undef': 0,不建议在TS文件中使用no-undef,TS会自行检查undef