Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why ESLint throws 'no-unused-vars' for TypeScript interface?

So, I have this piece of code in .ts file:

import {MicroEventInterface} from '../Interfaces';  export default class MicroEvent implements MicroEventInterface {  // code 

And ESLint throws this error:

error

I have this config for TypeScript in ESLint:

typescript: {     extends: [         'plugin:@private/private/react' // private rep with React config     ],     parser: '@typescript-eslint/parser',     plugins: [         '@typescript-eslint',         'import'     ],     settings: {         'import/resolver': {             'node': {                 'extensions': [                     '.js',                     '.jsx',                     '.ts',                     '.tsx'                 ],                 'moduleDirectory': [                     'node_modules/',                     'src/'                 ]             }         },         react: {             createClass: 'createClass',             pragma: 'React',             version: '0.14.9'         }     } } 

So, everything seems like fine, but I can't conquer this error.

Any suggestions?

Thanks!

UPD:

Looks like if I console.log( --- , MicroEventInterface); error disappears. I think, ESLint does not treat implements as actual usage.

like image 613
Nikita Shchypyplov Avatar asked Apr 23 '19 08:04

Nikita Shchypyplov


People also ask

How do you ignore no-unused-vars ESLint?

To apply the ESLint no-unused-vars rule to a block of JavaScript code, we can wrap the code block that we want to apply the rule to with /* eslint-disable no-unused-vars */ and /* eslint-enable no-unused-vars */ respectively. to wrap the code that we want the rule to apply with the comments.

Can ESLint be used for TypeScript?

ESLint is a JavaScript linter that you can use to lint either TypeScript or JavaScript code.

How do you remove unused variables from ESLint?

To automatically remove unused imports, we will need to add the eslint-plugin-unused-imports plugin. Now, when you run ESLint, you should see error lines saying error '<imported-var>' is defined but never used unused-imports/no-unused-imports for the files where you have unused imports.

What does TypeScript Eslint parser do?

This option allows you to programmatically provide an array of one or more instances of a TypeScript Program object that will provide type information to rules.


2 Answers

Source: I am the maintainer of the typescript-eslint project.

The latest version of the @typescript-eslint tooling now has full support for scope analysis.

So the steps to fix this are now:

  • update your versions to at least v4.9.1 of @typescript-eslint/parser and @typescript-eslint/eslint-plugin
  • update your ESLint version to at least v6.0.0
  • update your config to use @typescript-eslint/no-unused-vars
    • Do not use the base no-unused-vars rule - see this FAQ article in the project.

Restart your IDE and you should now see the correct lint errors.

like image 187
Brad Zacher Avatar answered Oct 15 '22 04:10

Brad Zacher


Use @typescript-eslint/no-unused-vars-experimental and turn off @typescript-eslint/no-unused-vars.

The issue is resolved in the latter version (experimental).

Sample config:

// .eslintrc {   ...   "rules": {     "@typescript-eslint/no-unused-vars": "off",     "@typescript-eslint/no-unused-vars-experimental": "error"   }   ... }  
like image 44
Christopher Regner Avatar answered Oct 15 '22 06:10

Christopher Regner