21

Is there a way to detect unused variables in Typescript (something like ESLint in Javascript)?

Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
Damjan Pavlica
  • 31,277
  • 10
  • 71
  • 76

3 Answers3

59

As of version 2.0, Typescript has built-in support for detecting unused local variables and parameters. The compiler flags are as follows:

   --noUnusedLocals                    Report Errors on Unused Locals.
   --noUnusedParameters                Report Errors on Unused Parameters.
Taytay
  • 11,063
  • 5
  • 43
  • 49
  • 16
    What if you just want these as warnings? – Izhaki Sep 07 '17 at 11:52
  • 1
    In that case you could use ESLint and configure it to emit a warning instead of an error for this rule: https://eslint.org/docs/rules/no-unused-vars – Taytay May 24 '20 at 17:24
  • I didn't get How should I use the above comments. I am sorry I am new to this Angular stuff. can you help me to say how to use this comments ? – Raghu Krishnan R Aug 07 '20 at 15:37
18

You can also detect unused variables in Typescript by updating the project’s tsconfig.json file to include noUnusedLocals and noUnusedParameters:

{
  "compilerOptions": {
    "noUnusedLocals": true,
    "noUnusedParameters": true
  }
}

Beau Smith
  • 33,433
  • 13
  • 94
  • 101
12

You can use TSLint instead.

https://palantir.github.io/tslint/

There's a rule for that: https://palantir.github.io/tslint/rules/no-unused-variable/

Edit:

Although this works, if you are using TypeScript 2 +, the compiler flags/options mentioned in the other answers.

Beau Smith
  • 33,433
  • 13
  • 94
  • 101
thitemple
  • 5,833
  • 4
  • 42
  • 67