简体   繁体   中英

Typescript eslint disable no-unused-vars

I am working on a Typescript React project and I usually put placeholder variables into the code so everything is laid out until I get to implementing everything. This leads to a bunch of eslint no-unused-vars errors and makes finding real errors a challage.

How can I disable this globally until I am ready for it? I used create-react-app my-app --typescript and don't want to eject the project but not sure how to disable this warning.

I noticed there is a eslintConfig section in the package.json so I tried to turn off the error there but it doesn't seem to work, is there a command I need to run after editing the package.json or is my syntax incorrect?

"eslintConfig": {
    "extends": "react-app",
    "rules": {
      "no-unused-vars": "off"
    }
 },

Update


(removed tsconfig.json reference)

I updated my package.json and keep getting the errors.

"eslintConfig": {
    "extends": "react-app",
    "rules": {
      "@typescript-eslint/no-unused-vars": "off"
    }
  },

I tried moving the rules into a .eslintrc.json file in the root of the project and still doesn't seem to turn this off.

The only thing that seems to work is putting // eslint-disable-line @typescript-eslint/no-unused-vars after the variable.

I think there is some confusion.

Both the question and the only answer suggest putting a rules section in the tsconfig.json . This is something I've never heard of, and it is not mentioned in the docs or in theofficial schema .

Also, the question is about disabling the rule in eslint so the rule change should go in the .eslintrc.json file (or the eslintConfig section in the package.json as considered in the question).

But this is typescript and typescript-eslint so the rule should be:

> "@typescript-eslint/no-unused-vars" : "off"

And the hacky solution proposed in an answer that someone linked to needs to be changed to (note that answer is about eslint for js, not ts).

/* eslint-disable @typescript-eslint/no-unused-vars */

And those solutions work for me using...

+-- eslint@6.5.1  
+-- @typescript-eslint/eslint-plugin@2.3.2  
+-- @typescript-eslint/parser@2.3.2  

Since you used create-react-app my-app --typescript there should already be tsconfig.json created for you in your my-app/

In your tsconfig.json you can add rules for your typescript compiler.

{
  "extends": [...],
  "compilerOptions": {
    ...
  },
  "include": [...],
  "rules": {
    ...
    "no-unused-vars": "off"
    ...
  }
}

Inside eslintrc.json add "rules" inside that you just have to write no-unused-vars to 0.

"rules":{ "no-unused-vars":0 }

I had a similar problem. Here is my solution:

  1. rename .eslintrc[.json] to .eslintrc.js
  2. add module.exports = infront of previous Json object. So .eslintrc.js looks like that:
module.exports = { 
    ...
    "rules": {...},
    ... 
}
  1. set "@typescript-eslint/no-unused-vars" rule depending on your current environment. This can be achieved by checking the process.env.NODE_ENV variable. My choice was to get an error in production and be warned in other cases. The snippet looks like this:
module.exports = { 
    ...
    "rules": {
         ...
         "@typescript-eslint/no-unused-vars": process.env.NODE_ENV === "production" ? "error" : "warn"
    },
    ... 
}

if you are using Create-react-app, there is no need to install anything or Eject, you just need to go to /node_modules/react-scripts/config/webpack.config.dev.js. on "new ESLintPlugin" rules just add:

'react/jsx-uses-react': 'error',
'react/jsx-uses-vars': 'error',
'no-unused-vars': 0

You can Add rule in package.json file

"eslintConfig": {
  "rules": {
   "no-unused-vars": 0,
  "@typescript-eslint/no-unused-vars": 0
}

Go into your settings and search for 'eslint', then look for something called Elint:enable, uncheck the box which states 'Controls whether eslint is enabled or not.'

It worked for me I am using Version: 1.72.2 (system setup) Commit: d045a5eda657f4d7b676dedbfa7aab8207f8a075 Date: 2022-10-12T22:15:18.074Z Electron: 19.0.17 Chromium: 102.0.5005.167 Node.js: 16.14.2 V8: 10.2.154.15-electron.0 OS: Windows_NT x64 10.0.19045 Sandboxed: No

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