Search code examples
typescripteslint

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?


Solution

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

    These are all standalone rules, but they've also all been integrated into Typescript-ESLint's official recommended-type-checked config.

    export default tseslint.config(
      eslint.configs.recommended,
      ...tseslint.configs.recommendedTypeChecked,
      ...tseslint.configs.stylisticTypeChecked,
    );
    

    Check here for other possibilities of config definition.

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