【问题标题】:Eslint Typescript "No Implicit Any" ruleEslint Typescript“无任何隐含”规则
【发布时间】:2020-11-13 15:11:29
【问题描述】:

我正在使用 eslint 建立一个新的打字稿项目。我正在尝试正确设置 eslint 规则,以便 tsc 命令运行时不会出错。我在 tsconfig.json 中使用的“noImplicitAny”规则有问题,但我无法在 eslint 中检查。

.eslintrc.js:

module.exports = {
    extends: [
        "eslint:recommended",
        "plugin:@typescript-eslint/eslint-recommended",
        "plugin:@typescript-eslint/recommended",
    ],
    parser: "@typescript-eslint/parser",
    parserOptions: {
        project: ["tsconfig.json"],
        sourceType: "module",
    },
    rules: {
        "no-undef": "warn",
    },
    plugins: ["@typescript-eslint"],
    settings: {
        "import/resolver": {
            node: {
                extensions: [".js", ".ts"],
            },
        },
    },
};

tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",
    "module": "ES6",
    "declaration": true,
    "outDir": "./lib",
    "strict": true,
    "noImplicitAny": true
  },
  "include": ["src/**/*.ts"],
  "exclude": ["node_modules", "**/__tests__/*"]
}

简而言之,我希望 eslint 检查隐含的 any 并警告其使用情况。如何配置.eslintrc.js 中的规则来实现这一点?

【问题讨论】:

  • 我已经检查了那个线程,它建议在 .eslintrc 中设置noImplicitAny,但是在 ESLint 中不存在这样的规则。我知道有'@typescript-eslint/no-explicit-any',但这不是我要找的。​​span>

标签: typescript eslint


【解决方案1】:

TypeScript-ESLint 中已经实现了许多no-unsafe-* 规则:

这些都是独立的规则,但它们也都被集成到 Typescript-ESLint的官方 recommended-requiring-type-checking 配置。

extends: [
    'plugin:@typescript-eslint/recommended-requiring-type-checking',

在类似的注释中,还有no-explicit-any。将所有这些放在一起,您应该可以完全避免any 问题。

【讨论】:

    猜你喜欢
    • 2021-04-14
    • 2019-11-10
    • 2020-11-10
    • 1970-01-01
    • 2019-09-04
    • 2020-10-01
    • 2021-11-21
    • 1970-01-01
    • 2020-06-04
    相关资源
    最近更新 更多