cham
cham

Reputation: 10732

Disable typescript-eslint plugin rule (no-explicit-any) with inline comment

I have an eslint error that comes from the @typescript-eslint plugin.

Unexpected any. Specify a different type.eslint(@typescript-eslint/no-explicit-any)

This is the no-implicit-any rule. In just one file I want to disable that rule with a comment at the top of the file.

The compiler complains if I just try a standard eslint disable:

/* eslint-disable  no-explicit-any */

Definition for rule 'no-explicit-any' was not found.eslint(no-explicit-any)

I've tried to find documentation on inline rules for the TS plugin, but without much luck. I've also tried various combinations like these:

/* typescript-eslint-disable no-implicit-any */
/* typescript-eslint: disable no-implicit-any */
/* typescript-eslint: disable noImplicitAny */
/* typescript-eslint-disable @typescript-eslint/no-implicit-any */

There are no eslint complaints but the error will not disappear.

How do I disable an typescript-eslint rule?

Upvotes: 190

Views: 269195

Answers (6)

Fellipe Sanches
Fellipe Sanches

Reputation: 8135

Why this error? The any type in TypeScript is a dangerous "escape hatch" from the type system. Using any disables many type checking rules and is generally best used only as a last resort or when prototyping code.

How to disable this check:

Open your eslint config file, in my case eslint.config.mjs file, adjust or add in your eslintConfig variable this code:

  ...compat.config({
    extends: ['next'],
    rules: {
      "@typescript-eslint/no-explicit-any": "error"
    },
  }),

E.g. of complete code:

const eslintConfig = [
  ...compat.extends("next/core-web-vitals", "next/typescript"),
  ...compat.config({
    extends: ['next'],
    rules: {
      "@typescript-eslint/no-explicit-any": "error"
    },
  }),
];

Adapt your configuration file according to its type: .js, .mjs or .cjs

source: https://typescript-eslint.io/rules/no-explicit-any/

Upvotes: 0

francis billa
francis billa

Reputation: 1

You can use the following solution:

  1. Turn off any inside the file .eslintrc.json. As done below, this allow you to declare it globally for the project.

rules: {"@typescript-eslint/no-explicit-any": "off" },

  1. I prefer this method because you explicitly opt-out in a file. so you simply declare it on the first line in your file. /* eslint-disable @typescript-eslint/no-explicit-any */

Upvotes: -1

abdelhedi hlel
abdelhedi hlel

Reputation: 3709

add this to the .eslintrc (tslintrc) file :

rules: {
    "@typescript-eslint/no-explicit-any": "off"
},

If you see this under VSCode as well restart ESLint server via command pallette: CTRL-Shift-P -> type eslint restart, click the respective dropdown item.

Upvotes: 238

Abdush Samad Miah
Abdush Samad Miah

Reputation: 316

You may also come across the following syntax, so you would update with value of 0 if it is set to 1:

'@typescript-eslint/no-explicit-any': 0,

Upvotes: -2

Meredith Murfin
Meredith Murfin

Reputation: 1381

If you're attempting to disable a rule on the next line only (rather than for an entire file), adding this comment directly above the line to ignore will work:

// eslint-disable-next-line  @typescript-eslint/no-explicit-any

Upvotes: 136

cham
cham

Reputation: 10732

The correct syntax is like this:

/* eslint-disable  @typescript-eslint/no-explicit-any */

So that you directly reference the plugin via eslint.

Upvotes: 241

Related Questions