How to detect unused variables in Typescript?

tslint no-unused-variable
typescript unused variable underscore
typescript/no-unused-vars
typescript-eslint
angular find unused components
tslint-etc
tslint unused variable underscore
tslint variable unused

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

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.

no-unused-variable, It would be nice to have unused detection expanded, capable of detecting there is no distinction in typescript unused variables from used� ) * Fix prepending unused TypeScript variables with underscore doesn't rename JSDoc @param. Fix test for quick fix "Prefix all unused declarations with '_' where possible". Fixes #33021. * Replace FindAllReferences.Core.eachSymbolReferenceInFile function call to more ligher call of getJSDocParameterTags when searching for a parameter in jsdoc.

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.

Show unused public properties and methods � Issue #29293 , I want to detect unused exports because they usually indicate bugs, or leftover code from language service fixer for unused variables Today's VS Code tip: nused variable highlighting JavaScript and TypeScript unused variables, arguments, and private properties/methods are rendered faded out. Use a quick fix to quickly remove them.

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

Rule proposal: Detect unused exports � Issue #371 � typescript-eslint , Use Cases Can be used to clearly state unused This wouldn't be a breaking change in existing TypeScript/JavaScript code above, but rather to ensure that we find a lasting and viable solution to unused parameters. Having a _ prefix on the variable name works nice with --noUnusedParameters . Note that the inferred type variables (in this case, U) can only be used in the true branch of the conditional type. A long-standing feature request for TypeScript has been the ability to extract the return type of a given function. Here's a simplified version of the ReturnType<T> type that's predefined in lib.es5.d.ts.

Revisiting _ underscore for unused parameters: create `unused , Deprecate no-unused-variable rule for typescript@next; also fix runtime Check if you have "no-unused-variable" right in there or in any of� Variable Declarations Variable Declarations #. let and const are two relatively new types of variable declarations in JavaScript.As we mentioned earlier, let is similar to var in some respects, but allows users to avoid some of the common “gotchas” that users run into in JavaScript.

no-unused-variable is deprecated. � Issue #4046 � palantir/tslint , It is important to double-check all files for correctness once the fix process all *. ts files in a given project and remove all unused TypeScript imports. this will throw tslint errors on unused imports, parameters and variables. microsoft / TypeScript. Watch 2.1k Star Do not report unused identifier errors for catch variables * getVarDate should be on the Date interface * Defere checking

Automatically Remove All Unused Imports in a TypeScript Project , These code inspections help you detect code issues in design time in all open files, and Introducing variable will allow to apply type guard the warning severity level, which detect empty and unused symbol declarations. TypeScript version: 2.9.2; Running TSLint via: CLI; Actual behavior "no-unused-variable is deprecated. Since TypeScript 2.9. Please use the built-in compiler checks instead." Expected behavior. This rule should not be deprecated. The TypeScript compiler check is not a replacement for a linter.

Comments
  • What if you just want these as warnings?
  • In that case you could use ESLint and configure it to emit a warning instead of an error for this rule: eslint.org/docs/rules/no-unused-vars
  • This is the way forward!
  • It is, as I said after editing the response, you should use the solution proposed by @Taytay
  • In light of the community's desire to keep this rule, it has been UNDEPRECATED! woop woop! github.com/palantir/tslint/pull/2256