ESLint - 配置“no-unused-vars"用于打字稿 [英] ESLint - Configuring "no-unused-vars" for TypeScript

查看:101
本文介绍了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 填充我的所有文档 here 似乎也是一个糟糕的解决方案.

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

      推荐答案

      有点埋在 文档,但是如果你在 'extends' 属性中添加一些东西,你可以使用 ESLint 推荐的规则,比如 no-unused-vars,并让它实际工作在打字稿中.像这样:

      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天全站免登陆