Rule: no-unused-variable

Disallows unused imports, variables, functions and private class members. Similar to tsc’s –noUnusedParameters and –noUnusedLocals options, but does not interrupt code compilation.

In addition to avoiding compilation errors, this rule may still be useful if you wish to have tslint automatically remove unused imports, variables, functions, and private class members, when using TSLint’s --fix option.

Rationale

Variables that are declared and not used anywhere in code are likely an error due to incomplete refactoring. Such variables take up space in the code, are mild performance pains, and can lead to confusion by readers.

Notes:
TS Only Has Fixer Requires Type Info

Config

Three optional arguments may be optionally provided:

  • "check-parameters" disallows unused function and constructor parameters.
    • NOTE: this option is experimental and does not work with classes that use abstract method declarations, among other things.
  • {"ignore-pattern": "pattern"} where pattern is a case-sensitive regexp. Variable names and imports that match the pattern will be ignored.
Config examples
"no-unused-variable": true
"no-unused-variable": [true, {"ignore-pattern": "^_"}]
Schema
{
  "type": "array",
  "items": {
    "oneOf": [
      {
        "type": "string",
        "enum": [
          "check-parameters"
        ]
      },
      {
        "type": "object",
        "properties": {
          "ignore-pattern": {
            "type": "string"
          }
        },
        "additionalProperties": false
      }
    ]
  },
  "minLength": 0,
  "maxLength": 3
}