Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 465
zithir Avatar asked Sep 07 '25 16:09

zithir


1 Answers

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

  • no-unsafe-call - forbids calling an expression typed as any
  • no-unsafe-member-access - forbids using any as a member name
  • no-unsafe-argument - forbids passing any as a function parameter
  • no-unsafe-assignment - forbids using any in an assignment

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.

like image 96
CertainPerformance Avatar answered Sep 10 '25 06:09

CertainPerformance