How to detect unused variables in Typescript? How to detect unused variables in Typescript? typescript typescript

How to detect unused variables in Typescript?


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.


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.


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  }}