Tslint: Warning: The 'no-unused-variable' rule requires type checking message...

Created on 16 Jun 2017  ·  46Comments  ·  Source: palantir/tslint

Bug Report

with tslint.json configuration:

{
  "rulesDirectory": [
    "node_modules/codelyzer"
  ],
  "rules": {
    "arrow-return-shorthand": true,
    "callable-types": true,
    "class-name": true,
    "comment-format": [
      true,
      "check-space"
    ],
    "curly": true,
    "eofline": true,
    "forin": true,
    "import-blacklist": [
      true,
      "rxjs"
    ],
    "import-spacing": true,
    "indent": [
      true,
      "spaces"
    ],
    "interface-over-type-literal": true,
    "label-position": true,
    "max-line-length": [
      true,
      140
    ],
    "member-access": false,
    "member-ordering": [
      true,
      {
        "order": [
          "public-static-field",
          "protected-static-field",
          "public-static-method",
          "protected-static-method"
        ]
      }
    ],
    "no-arg": true,
    "no-bitwise": true,
    "no-console": [
      true,
      "debug",
      "info",
      "time",
      "timeEnd",
      "trace"
    ],
    "no-construct": true,
    "no-debugger": true,
    "no-duplicate-super": true,
    "no-empty": false,
    "no-empty-interface": true,
    "no-eval": true,
    "no-inferrable-types": [
      true,
      "ignore-params"
    ],
    "no-unused-variable": true,
    "no-misused-new": true,
    "no-non-null-assertion": true,
    "no-shadowed-variable": true,
    "no-string-literal": false,
    "no-string-throw": true,
    "no-switch-case-fall-through": true,
    "no-trailing-whitespace": true,
    "no-unnecessary-initializer": true,
    "no-unused-expression": true,
    "no-var-keyword": true,
    "object-literal-sort-keys": false,
    "one-line": [
      true,
      "check-open-brace",
      "check-catch",
      "check-else",
      "check-whitespace"
    ],
    "prefer-const": true,
    "quotemark": [
      true,
      "single"
    ],
    "radix": true,
    "semicolon": [
      "always"
    ],
    "triple-equals": [
      true,
      "allow-null-check"
    ],
    "typedef-whitespace": [
      true,
      {
        "call-signature": "nospace",
        "index-signature": "nospace",
        "parameter": "nospace",
        "property-declaration": "nospace",
        "variable-declaration": "nospace"
      }
    ],
    "typeof-compare": true,
    "unified-signatures": true,
    "variable-name": false,
    "whitespace": [
      true,
      "check-branch",
      "check-decl",
      "check-operator",
      "check-separator",
      "check-type"
    ],
    "directive-selector": [
      true,
      "attribute",
      "app",
      "camelCase"
    ],
    "component-selector": [
      true,
      "element",
      "app",
      "kebab-case"
    ],
    "use-input-property-decorator": true,
    "use-output-property-decorator": true,
    "use-host-property-decorator": true,
    "no-input-rename": true,
    "no-output-rename": true,
    "use-life-cycle-interface": true,
    "use-pipe-transform-interface": true,
    "component-class-suffix": true,
    "directive-class-suffix": true,
    "no-access-missing-member": true,
    "templates-use-public": true,
    "invoke-injectable": true
  }
}

What exactly am I doing wrong?

  • __TSLint version__: 5.4.3
  • __TypeScript version__: 2.3.4
  • __Running TSLint via__: yarn and command line

Actual behavior

When running tslint with the below parameters:

tslint --type-check --project tsconfig.json  src/**/*.ts

I get the below warning:

Could not find implementations for the following rules specified in the configuration:
    Warning: The 'no-unused-variable' rule requires type checking
Try upgrading TSLint and/or ensuring that you have all necessary custom rules installed.
If TSLint was recently upgraded, you may have old rules configured which need to be cleaned up.

Expected behavior

According to the tslint documentations to do type checking all I need to do is pass in --type-check --project tsconfig.json but it does not seem to work.

Question

Most helpful comment

If you look at VSCode 1.19 readme, there is a new feature for trating this and other rules as warnings.
For us, it means:

  • remove "no-unused-variable" from tslint.json
  • add "noUnusedLocals" to our tsconfig.json

Other rules are left as an exercise for the reader ;-)

Note, that the tslint is this extension, while tsconfig is the VSCode built-in behavior.

All 46 comments

Please fill out the issue template. What does your tslint.json look like?

I am home and tried the same project and its working fine. I will leave this open until I get to work on Monday to test again. Might need to just reinstall my node modules.

I have also updated to use the issue template.

Sometime after tslint updated (maybe v4 -> v5), no-unused-variable are not available without --type-check, --project arguments.

This option hard to use and take long time until print results after updated, but important and most used linting option. is there any way to use this options without upper arguments?

Sometime after tslint updated (maybe v4 -> v5), no-unused-variable are not available without --type-check, --project arguments.

I am passing in --project with tslint --type-check --project tsconfig.json src/**/*.ts

On my home computer I'm not having this issue but on my work computer I am.

@everedifice there were bugs with the rule implementation, we almost deprecated it (https://github.com/palantir/tslint/issues/1481), and ultimately decided to keep it by delegating to the compiler's implementation of no-unused-variable (this addressed most of the bugs). There are lots of discussion threads about it in this repo that I'd encourage you to check out, mostly linked from the PR that made the breaking change (https://github.com/palantir/tslint/pull/2235.

@mastrauckas I can't really help you without more details about the differences between your two different environments.

@adidahiya I was getting this on a third computer which is a macOS last night too. I am going to try it again on my home computer again to see if it still happens.

In general:
computer 1: Windows 10 completely up to date and as of right now I'm not getting the error.
computer 2: macOS completely updated and I get the error.
computer 3: Windows 7 completely updated and I get the error.

@everedifice I just published a new version of tslint-consistent-codestyle with a new rule no-unused which is mostly like no-unused-variable with some additional features but without the need to use --project --type-check.

If you're interested, you can find the docs here: https://github.com/ajafff/tslint-consistent-codestyle/blob/master/docs/no-unused.md

The computer I thought was not having the problem appears to be having the issue after all. Is this a bug?

Anything new on this issue?

I'm having the same issue.

The same issue, but with both 'Warning: The 'no-unused-variable' rule requires type information.'
and 'Warning: The 'no-use-before-declare' rule requires type information.' (I can see that typo in 'information' was fixed between version 5.5.0 and 5.7.0, but that's the only thing that changed). I am running it with typescript version 2.4.1. Any news on that problem?

Same issue here

Is this issue resolved or any temporary fix to stop displaying warnings.

I don’t think it’s fixed. I don’t even think they accepted as an issue.

Permanent fix, use no-unused-variable in tsconfig.json instead of tslint.json. If the compiler can provide the same functionality, it doesn't make any sense to do it with a linter.

@AnimaMundi well it does provide the same functionality however when running tslint as part of our CI process these needs to be flagged during test, not during build. I disaggree that this should be ignored / removed because the compiler does this at build time.

@haswalt I don't see any reason why it needs to be flagged during the linting phase rather than the build phase. Anyway, it doesn't matter if you agree with me or not, I've nothing to do with the development of this project. I just seen that someone was looking for a fix and provided them with it.

Similar to @AnimaMundi's fix, I used noUnusedLocals and noUnusedParameters in my tsconfig.json. Errors show up in my "problems" pane.

@AnimaMundi @haswalt @keego the merits of a lint rule vs. a compiler check for unused vars has been discussed extensively in other TSLint threads (https://github.com/palantir/tslint/issues/1481 and linked threads); this issue is not really the place for it.

@adidahiya my comment was merely meant to share a fix for this issue, not provide commentary on the merits of linter rules vs compiler checks.

Deferring to the compiler doesn't do the same thing: compiler has only one option: FAIL.
The reason for tslint to handle it is that it often needs to be a warning (green squiggle vs red one).

not only can tslint problems be shown as warnings but they can also be ignored on a one off basis, whereas typescript compile errors cannot be

These points have already all been hashed out (see my last comment and the linked threads). There is nothing stopping you from using no-unused-variable as a TSLint rule right now. I'm not sure what these follow up comments are trying to achieve.

@mastrauckas are you still having issues with this rule? The --type-check flag has been deprecated, but --project is required to enable type checking based rules.

Aside: with TS 2.6, you can use // @ts-ignore to suppress compiler errors.

@adidahiya i was merely responding to the comments above that claim there's no reason to have this as a lint rule rather than a compiler rule.

but you're right i guess the relevant issue is with the vscode extension not tslint itself. tbh there were so many issues on this topic that it took me a while to suss out exactly why it's not working and where.

appreciate the tip. i will def switch back to using the compiler setting as soon as we switch to 2.6

Another point for those who claim this should not be a lint rule: lint warnings can be fixable, and this is a prime candidate for that.

I getting the same warning in the latest 1.19 VS Code The 'no-unused-variable' rule requires type information with this (see below). Is my configuration missing something?

{
  "extends": "tslint:recommended",
  "rules": {
    "linebreak-style": [true, "LF"],
    "quotemark": [true, "single", "avoid-escape", "avoid-template"],
    "no-console": false,
    "no-unused-expression": false,
    "ordered-imports": false,
    "member-access": [true, "no-public"],
    "object-literal-sort-keys": false,
    "curly": [true, "ignore-same-line"],
    "semicolon": [true, "never"],
    "no-var-requires": false,
    "no-unused-variable": true
  }
}

If you look at VSCode 1.19 readme, there is a new feature for trating this and other rules as warnings.
For us, it means:

  • remove "no-unused-variable" from tslint.json
  • add "noUnusedLocals" to our tsconfig.json

Other rules are left as an exercise for the reader ;-)

Note, that the tslint is this extension, while tsconfig is the VSCode built-in behavior.

I've opted to remove no-unused-variable since VS 1.19 seems to report those already.

I don't understand what VSCode has to do with this. It's not enough to have only VSCode treat these as warnings, I need my webpack dev server watch build not to fail because of them. Is there a replacement for no-unused-variable that, independent of particular editors, will

  • be treated as a warning during development
  • fail builds during CI

?

@pelotom , several people got redirected here from VSCode TSLint extension, e.g. from https://github.com/Microsoft/vscode-tslint/issues/219

Until now, TS was not able to show some issues as warnings (green squiggles), but lints (ESlint, TSLint) were able to. So we were asking why TSlint would not report unused variables as warnings. But as of 1.19, VSCode's TS can make these warnings and we are withdrawing from this issue 😉

To be clear, I use VSCode too and would also like to see these warnings highlighted in the editor, but VSCode's settings seem like the wrong layer at which to be _configuring_ the severity of compiler errors, since it doesn't affect the rest of your toolchain.

I agree with you Tom, and hope the solution will be found where both the editor as well as our build/CI procedures would have the proper support. So far we have seen strong pushback (seemingly justified) and some (incl. me) would take what we can, today, while keeping hope for real solution.

If you look at VSCode 1.19 readme, there is a new feature for trating this and other rules as warnings.

@rnemec I think you meant to write "release notes" instead of "readme". I was stupid enough to look for the readme. And for anyone arriving here we might as well quote the relevant bit:

VS Code now displays TypeScript code style issues as warnings instead of errors. This applies to:

Variable is declared but never used
Property is declared but its value is never read
Unreachable code detected
Unused label
Fall through case in switch
Not all code paths return a value
Treating these as warnings is consistent with other tools, such as TSLint. These will still be displayed as errors when you run tsc from the command line.

You can disable this behavior by setting: "typescript.reportStyleChecksAsWarnings": false.

So this is a temporary solution and would only work when using VSCode. Any other build tool using tsc directly would not compile the code if the warnings are left in there. I have some colleagues who are using Atom, so they won't be able to compile the code if I made changes and ignored one of the warnings. Right?

I don't quite understand how this solves anything. How is displaying these things as warnings useful if the compiler still treats them as errors?

And if I understand correctly, the long term solution would be to either add a flag to enforce the same behavior in tsc (making it compile with warnings), or remove these compiler checks completely?

--- edit ---
I didn't read through all the previous comments. @pelotom was already making the same point.

If you want lint checks to have different severity in dev mode vs. CI mode, you can do this with multiple tslint.json configs where one extends the other. #2569 needs to be fixed for the UX here to be good so that you can toggle the severity with just one defaultSeverity config line -- it's in progress.

@adidahiya what's being discussed is a purported _replacement_ for the no-unused-variable lint rule, using tsc's noUnusedLocals.

@pelotom right, I got that -- but I don't think tsc supports such a workflow yet. You could have two tsconfig.json files where one extends the other and disables each "non-fatal" compiler check (in this case, noUnusedLocals, noUnusedParameters) individually, however this wouldn't let you see the checks as warnings in your editor.

@adidahiya yes, that's why it's not a valid replacement currently. And I'd much rather see no-unused-variable made to work properly in VSCode than invest more effort getting noUnusedLocals to behave more like a lint rule. IMO it was a mistake for the compiler to try to take ownership of this sort of check in the first place.

And I'd much rather see no-unused-variable made to work properly in VSCode than invest more effort getting noUnusedLocals to behave more like a lint rule.

Makes sense. I believe this project aims to do that: https://github.com/angelozerr/tslint-language-service. You can use rules that require type checking with that plugin.

I spoke too soon. We're blocked on https://github.com/palantir/tslint/issues/2649 for that plugin to be useful. Track that issue instead.

@adidahiya yep, I've been tracking it for quite some time.

Any news on this? Will it be deprecated or not?

@philip-firstorder using this fixed the issue for me --project tsconfig.json

tslint -c tslint.json --project tsconfig.json src/**/*.ts

no-unused-variable is now deprecated, see #3918 and #3919

🤖 Beep boop! 👉 TSLint is deprecated 👈 and you should switch to typescript-eslint! 🤖

🔒 This issue is being locked to prevent further unnecessary discussions. Thank you! 👋

Was this page helpful?
0 / 5 - 0 ratings