60

While working on a TypeScript project, I commented out a line, and got the error:

Failed to compile

./src/App.tsx
(4,8): error TS6133: 'axios' is declared but never used.

This error occurred during the build time and cannot be dismissed.

The error is right, I am importing axios, but I wanted to temporarily comment out the call to axios.get. I appreciate that error as it keeps my imports clean, but during early development is is pretty disruptive.

Any way to disable or ignore that warning?

jdm
  • 9,470
  • 12
  • 58
  • 110

3 Answers3

88

You probably have the noUnusedLocals compiler option turned on in your tsconfig.json. Just turn it off during development.

Saravana
  • 37,852
  • 18
  • 100
  • 108
  • 3
    In my case I also forgot to restart `npm run start` for changes to take effect. – Sergey Yarotskiy Jan 26 '18 at 19:25
  • It won't if you disable `noUnusedLocals` in the `tsconfig.json` file you are using for buildiing. – Saravana Mar 06 '18 at 01:43
  • 23
    can this be set as a warning? – Guillermo Ruffino Mar 08 '19 at 19:31
  • @GuillermoRuffino - I sort of agree -- making this a warning would be create mildly annoying warnings in modern editors, but making it a compiler error ensures that you only ever build 'clean code'. – Xaero Degreaz Apr 08 '19 at 01:33
  • 1
    If anyone else is still getting the errors after changing the option make sure there aren't any other tsconfig files in your project. If you're using vscode open the command palette (ctrl + p) and start typing tsconfig to find other files. I had a tsconfig.app.json that was overriding the one at the root – Crhistian Ramirez Aug 16 '19 at 13:07
  • this warning it only serves to remember not to use variables without being used, but this can be false positive, because sometimes we use the variables inside '' and even then the error is returned. – luke cross Aug 12 '20 at 19:39
  • So changing the noUsedLocals field (tsconfig file) from true to false seems like a really good idea. – luke cross Aug 12 '20 at 19:39
  • 3
    This doesn't seem to work. By default `noUsedLocals` is false according to docs. However, omitting it still shows the error, and the error persists even with it being set to false. After TS server & vscode restart. There are no other tsconfig files either. – Douglas Gaskell Dec 16 '21 at 22:18
14

In tsconfig.json

{
  "compilerOptions": {
    ...
    "noUnusedLocals": false,   // just set this attribute false
  }
}

It will be done.

For more tips:

In xxx.ts file

//@ts-nocheck 
when on the top of the file,it will not check the below.

//@ts-ignore
when use it,it will not check the next line
xia2
  • 229
  • 3
  • 4
1

I had the same problem in my React App. Apart from changing the "noUsedLocals": false property in the tsconfig.json, you also need to adjust the "noUnusedParameters": false. The former is only applicable to local variables, if you are passing unused parameters through functions, the latter will need to be changed to false as well.

In summary, you'll have to do the following:

{
  "compilerOptions": {
     "noUnusedLocals": false,
     "noUnusedParameters": false,
 }
}
gohar-fse
  • 11
  • 1