esLint-配置“ no-unused-vars”用于打字稿 [英] esLint - Configuring "no-unused-vars" for Typescript

查看:443
本文介绍了esLint-配置“ no-unused-vars”用于打字稿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在所有具有以下设置的Typescript项目中使用esLint:

I use esLint in all of my Typescript projects with the following settings:

  "extends": ["airbnb", "prettier", 'plugin:vue/recommended'],
  "plugins": ["prettier"],
  "parserOptions": {
  "parser": "@typescript-eslint/parser",
  "ecmaVersion": 2018,
  "sourceType": "module"
  },

+一堆自定义规则。我还为Typescript支持安装了以下依赖项:

+ 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",

但是,这是esLint最有用的规则之一, https://eslint.org/docs / rules / no-unused-vars 对于Typescript项目似乎配置很差。例如,当我导出一个枚举时,该规则警告我该枚举在声明它的文件中没有使用:

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

类似地,当我导入要用作类型的接口或类, no-unused-vars将再次在实际导入的行上抱怨:

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:

在Foo.ts

export interface Foo {
   bar: string;
}

在bar.ts

import { Foo } from './Foo'
const bar: Foo = { bar: 'Hello' };

是否可以通过配置no-unused-vars规则来考虑这两种情况?我不喜欢禁用该规则,因为在这些情况下,它是我整个规则集中最有用的规则之一。

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.

我已经将规则降级为仅给出警告而不是错误,但是让我的所有文档都充满警告仍然无法达到使用esLint的目的。

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.

按照建议用// eslint-disable-line填充我的所有文档在这里似乎也很糟糕。

Filling my all my documents with //eslint-disable-line as suggested here also seems like a bad solution.

推荐答案

它有点埋在文档中,但是,如果您在 extends属性中添加了一些内容,则可以使用ESLint推荐的两个规则(如no-unused-vars),并使其在Typescript中实际起作用。像这样:

It's a bit buried in the documentation, but if you add some things to the 'extends' property, you can use both the rules recommended by ESLint like no-unused-vars, and have it actually work in Typescript. Like so:

"extends": [
        "eslint:recommended",
        "plugin:@typescript-eslint/eslint-recommended",
        "plugin:@typescript-eslint/recommended"
    ],

@ typescript-eslint / recommended似乎是允许eslint:recommended有效处理Typescript构造的东西。不确定如何影响其他扩展。

@typescript-eslint/recommended seems to be the thing that allows eslint:recommended to deal with Typescript constructs effectively. Not sure how it would affect your other extensions though.

这篇关于esLint-配置“ no-unused-vars”用于打字稿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆