Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSCode - gray out unused imports

Is it possible to configure VSCode so that it displays unused import as grayed? I have VSCode 1.21.0, using Typescript 2.7.2

I found and tried several setups but none worked for me. Based on this link it should have been possible since version 1.19.0

I tried setting tsconfig as described here but the only effect was lots of error during compilation.

It seems to be so basic requirement that I would even consider it as default setting. It is definitely possible in other editors (e.g. WebStorm). I love VSCode but I'm really missing this feature.

like image 781
eXavier Avatar asked Mar 13 '18 14:03

eXavier


2 Answers

For me the problem was that I had turned off javascript.validate.enable, so even though the editor.showUnused was set to true, it didn't work. So the fix for me was to have both:

{
  "javascript.validate.enable": true,
  "editor.showUnused": true
}
like image 121
Darko Maksimovic Avatar answered Sep 19 '22 15:09

Darko Maksimovic


This feature was added for JavaScript and TypeScript with VS Code 1.24

enter image description here

VS Code ships with built-in support for fading out unused locals/parameters/imports in JavaScript and TypeScript. You can enable/disable this feature by setting:

// For all languages (it is enabled the default)
"editor.showUnused": true

// Or just for a specific language
"[typescript]": {
   "editor.showUnused": true
}

Extensions can also add support for other languages.

You can additionally mark unused variables as warnings by setting noUnusedLocals and/or noUnusedParameters in your jsconfig or tsconfig:

{
  "compilerOptions": {
    "noUnusedLocals": true,
    "noUnusedParameters": true
  },
  "exclude": [
    "node_modules",
    "**/node_modules"
  ]
}

This adds a squiggly and error for unused variables in addition to graying them out:

enter image description here

like image 26
Matt Bierner Avatar answered Sep 18 '22 15:09

Matt Bierner