patterntypescriptCritical
ESLint - Configuring "no-unused-vars" for TypeScript
Viewed 0 times
typescriptvarsunusedforconfiguringeslint
Problem
I use ESLint in all of my TypeScript projects with the following settings:
-
a bunch of custom rules. I've also installed the following dependencies for TypeScript support:
However, one of ESLint's most useful rules, https://eslint.org/docs/rules/no-unused-vars, seems to be very poorly configured for TypeScript projects. For example, when I export an enum, the rule warns me that the enum isn't in use in the file where it is declared:
Similarly, when I import an interface or class to be used as a type, 'no-unused-vars' will complain again on the the line of the actual import:
In Foo.ts
In bar.ts
Is there any way to configure the no-unused-vars rule to take these two cases into account? I'm not a fan of disabling the rule, as it is one of the most helpful rules in my entire ruleset outside of these cases.
I've already downgraded the rule to only give a warning instead of an error, but having all my documents filled with warnings still kind of defeats the purpose of using esLint.
Filling my all my documents with //eslint-disable-line as suggested here also seems like a bad solution.
"extends": ["airbnb", "prettier", 'plugin:vue/recommended'],
"plugins": ["prettier"],
"parserOptions": {
"parser": "@typescript-eslint/parser",
"ecmaVersion": 2018,
"sourceType": "module"
},-
a bunch of custom rules. I've also installed the following dependencies for TypeScript support:
"@typescript-eslint/eslint-plugin": "^1.7.0",
"@typescript-eslint/parser": "^1.7.0",However, one of ESLint's most useful rules, https://eslint.org/docs/rules/no-unused-vars, seems to be very poorly configured for TypeScript projects. For example, when I export an enum, the rule warns me that the enum isn't in use in the file where it is declared:
export enum Foo {
Bar,
}Similarly, when I import an interface or class to be used as a type, 'no-unused-vars' will complain again on the the line of the actual import:
In Foo.ts
export interface Foo {
bar: string;
}In bar.ts
import { Foo } from './Foo'
const bar: Foo = { bar: 'Hello' };Is there any way to configure the no-unused-vars rule to take these two cases into account? I'm not a fan of disabling the rule, as it is one of the most helpful rules in my entire ruleset outside of these cases.
I've already downgraded the rule to only give a warning instead of an error, but having all my documents filled with warnings still kind of defeats the purpose of using esLint.
Filling my all my documents with //eslint-disable-line as suggested here also seems like a bad solution.
Solution
I think the use of
Note: be sure to restart your server after making the above change.
Reference - https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-unused-vars.md
"plugin:@typescript-eslint/eslint-recommended" introduces bunch of unwanted rules. One is probably better off using "@typescript-eslint/no-unused-vars" ESLint rule instead.{
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint",
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
],
"rules": {
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": ["error"]
}
}
Note: be sure to restart your server after making the above change.
Reference - https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-unused-vars.md
Context
Stack Overflow Q#57802057, score: 276
Revisions (0)
No revisions yet.