简体   繁体   English

解决 class 字段的“@typescript-eslint/no-invalid-this”问题,而不与“@typescript-eslint/no-this-alias”冲突

[英]Resolve “@typescript-eslint/no-invalid-this” issue for class fields without conflicting with “@typescript-eslint/no-this-alias”

In below code:在下面的代码中:

import { Vue, Component } from "vue-property-decorator";  


@Component({
  components: {}
})
export default class ProductViewingAndEditingPage extends Vue {

  private readonly componentsReferencesIDs: {
    productTitleInputField: string;
    productPriceInputControl: string;
  } = {
    productTitleInputField: "ProductTitle--InputField",
    productPriceInputControl: "ProductPrice--InputComponent"
  };

  private readonly productDataFormValidator: InputsGroupValidator = new InputsGroupValidator({
    controls: {
      [this.componentsReferencesIDs.productTitleInputField]:
          this.$refs[this.componentsReferencesIDs.productTitleInputField],
      [this.componentsReferencesIDs.productPriceInputControl]:
          this.$refs[this.componentsReferencesIDs.productPriceInputControl]
    }
  });
}

@typescript/eslint emits @typescript-eslint/no-invalid-this issue (because it's not ESLint's no-invalid-this , this rule understands the class fields.). @typescript/eslint发出@typescript-eslint/no-invalid-this问题(因为它不是 ESLint 的no-invalid-this ,所以该规则理解 class 字段。)。

Musings about conceptual solution关于概念解决方案的思考

If I make componentsReferencesIDs to static class field, it's become impossible to retrieve the values from vue template.如果我将componentsReferencesIDs设置为 static class 字段,则无法从 vue 模板中检索值。

Arrow functions could not be used here because here is class field.这里不能使用箭头函数,因为这里是 class 字段。 And also, if we alias this , we will face with no-this-alias rule.而且,如果我们给this加上别名,我们将面临no-this-alias规则。

I understand that ESLint is not the sole source of truth.我知道 ESLint 不是唯一的事实来源。 But I want the justification like "because 〈argument〉, this ESLint rule does not cover this case."但我想要像“因为〈argument〉,这个 ESLint 规则不包括这种情况”这样的理由。

@typescript-eslint/no-invalid-this just hasn't had support for this in class properties added. @typescript-eslint/no-invalid-this只是在添加的 class 属性中不支持this功能。

The extension rule was only added to the project relatively recently.扩展规则是最近才添加到项目中的。 At the time, the author worked on the feature they needed, which was support for this args.当时,作者致力于他们需要的功能,即对this args 的支持。

As a community maintained project, we rely upon support from the community to help us add rules, add features and fix bugs.作为一个社区维护的项目,我们依靠社区的支持来帮助我们添加规则、添加功能和修复错误。
If anyone wants to tackle this - please feel free to submit a PR - I believe that it should actually be a relatively simple fix.如果有人想解决这个问题 - 请随时提交 PR - 我相信它实际上应该是一个相对简单的修复。

Relevant issue on github: https://github.com/typescript-eslint/typescript-eslint/issues/491 github 上的相关问题: https://github.com/typescript-eslint/typescript-eslint/issues/491


As an aside, this probably begs the question "why hasn't this been fixed in over a year??"顺便说一句,这可能引出了一个问题:“为什么一年多没有解决这个问题??”

Two reasons:两个原因:

  1. When you have the noImplicitThis compiler option turned on, TypeScript itself will throw a compiler error if you're using an invalid this .Because it's handled by TS directly, the vast majority of users don't feel the need to duplicate the error with a lint rule.当你打开noImplicitThis编译器选项时,如果你使用无效的this ,TypeScript 本身会抛出编译器错误。因为它是由 TS 直接处理的,绝大多数用户不觉得有必要用 a 来复制错误皮棉规则。
  2. Not many users use this in class properties, meaning users aren't likely to run into this issue to begin with.没有多少用户在 class 属性中使用this ,这意味着用户不太可能一开始就遇到这个问题。 Some users don't like that style of properties, but I believe most users don't actually know that it's valid to do this.有些用户不喜欢这种风格的属性,但我相信大多数用户实际上并不知道这样做是有效的。

Putting these two together - few users use the lint rule, and fewer users still use this in class properties - so there hasn't been enough people to motivate the community to fix it.将这两个放在一起 - 很少有用户使用 lint 规则,并且在 class 属性中仍然使用this的用户更少 - 所以没有足够的人来激励社区修复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM