Is there a way to detect unused variables in Typescript (something like ESLint in Javascript)?
Asked
Active
Viewed 2.1k times
3 Answers
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
-
16What if you just want these as warnings? – Izhaki Sep 07 '17 at 11:52
-
1In 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
-
-
It is, as I said after editing the response, you should use the solution proposed by @Taytay – thitemple Mar 10 '18 at 13:36
-
In light of the community's desire to keep this rule, it has been *UNDEPRECATED*! woop woop! https://github.com/palantir/tslint/pull/2256 – protoEvangelion Nov 10 '18 at 23:25
-