简体   繁体   中英

Eslint Typescript "No Implicit Any" rule

I am setting up a new typescript project with eslint. I am trying to setup eslint rules correctly so that the tsc command runs without errors. I have the problem with the "noImplicitAny" rule, which I use in tsconfig.json , but I am unable check for this in 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__/*"]
}

In short I want the eslint to check for the implicit any and warn about its usage. How can I configure the rules in .eslintrc.js to achieve this?

There are a number of no-unsafe-* rules that have been implemented in TypeScript-ESLint:

On a similar note, there's no-explicit-any . Put all of these together, and you should be fully protected against any problems.

eslint refuses to add a no-implicit-any due to the fact it duplicates a typescript rule.

They did however make a transition rule that should not be used permanently which does what you wanted. You should remove this rule once you can turn these options on in your tsconfig file.

This allows warning for implicit parameters or arguments in normal and arrow functions. There are more options than the ones commented out, documentation linked below.

    '@typescript-eslint/typedef': [
      'warn',
      {
        parameter: true,
        arrowParameter: true,
        // variableDeclaration: true,
        // memberVariableDeclaration: true,
        // objectDestructuring: true,
      },
    ],

link to github issue not allowing no-implicit-any https://github.com/typescript-eslint/typescript-eslint/issues/3979

doucmentation for typedef information https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/docs/rules/typedef.md

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM