简体   繁体   English

错误 'this' 到局部变量 @typescript-eslint/no-this-alias 的意外别名

[英]ERROR Unexpected aliasing of 'this' to local variable @typescript-eslint/no-this-alias

How do I solve this typescript error?如何解决这个打字稿错误?

I'm getting error in the below line.我在下面的行中遇到错误。

const self = this;常量自我 = 这个;

I'm getting error in the terminal like:我在终端中遇到错误,例如:

error Unexpected aliasing of 'this' to local variable @typescript-eslint/no-this-alias错误 'this' 到局部变量 @typescript-eslint/no-this-alias 的意外别名

Please find the code below:请在下面找到代码:

notifyOwner(req, metadata, callback) {
    const currentUserId = req.session.identity.individual.userName;
    const docId = metadata.docId;

    let message = this.templates.selfRemoval;
    const self = this;

    const targetUserId = metadata.ownerId;
    const paperTitle = metadata.title.replace(/<(.|\n)*?>/g, '');
    const nonOwnerRole = metadata.userRole;

    message = message.replace(/\[CollaboratorName\]/g, req.session.identity.individual.firstName + ' ' + req.session.identity.individual.lastName);
    message = message.replace(/\[NonOwnerRole\]/g, (nonOwnerRole === 'author') ? 'collaborator' : 'reviewer');
    message = message.replace(/\[PaperTitle\]/g, paperTitle);

    const eventData = {
      message: message,
      objectType: 'manuscript',
      objectId: docId
    };

    self.createEvent(currentUserId, targetUserId, eventData, (err, result) => {
      if (result) {
        const userActivityService = new UserActivityService();
        userActivityService.broadcastRefreshUserEvents(eventData['objectId'], { userId: targetUserId });
      }

      callback(null, JSON.stringify({ notified: true }));
    });
  }

This warning usually exists to get script-writers to utilize arrow functions instead of declaring new variables.这个警告通常是为了让脚本编写者使用箭头函数而不是声明新变量。 For example, this:例如,这个:

let that = this;
someFn(function(arg) {
  return that.foo = arg;
});

can be simplified to可以简化为

someFn(arg => this.foo = arg);

But in your case, you're not even using the reassigned value in any other functions - you're just referencing it directly lower in the code, which makes the assignment completely superfluous.但是在您的情况下,您甚至没有在任何其他函数中使用重新分配的值 - 您只是在代码中直接引用它,这使得分配完全多余。

Just remove your只需删除您的

const self = this;

and replace并更换

self.createEvent(

with

this.createEvent(

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

相关问题 如何将 typescript-eslint 规则添加到 eslint - How to add typescript-eslint rules to eslint ESLint 无法识别“@typescript-eslint/eslint-plugin” - ESLint is not recognizing "@typescript-eslint/eslint-plugin" ESlint &amp; Angular 13: 无法关闭 typescript-eslint/typedef - ESlint & Angular 13: Unable to turn off typescript-eslint/typedef angular-eslint 和 typescript-eslint 之间的区别 - Difference between angular-eslint and typescript-eslint 为什么 typescript-eslint 强制 enumMember 使用驼峰命名法? - Why does typescript-eslint enforce camelCase for enumMember? Angular &amp; eslint - 未找到规则“@typescript-eslint/space-infix-ops”的定义 - Angular & eslint - Definition for rule '@typescript-eslint/space-infix-ops' was not found 使用“typescript-eslint/recommended”和“@angular-eslint/recommended”扩展多个规则集可以吗? - Extending multiple rule-sets using "typescript-eslint/recommended" with "@angular-eslint/recommended" is this okay to do? Typescript - 指定破坏变量别名的类型 - Typescript - specify type for destructed variable alias 语法错误 - 打字稿 - 意外标记:? - Syntax error - typescript - unexpected token :? 当我在Angular2 provider()别名中使用&#39;Token&#39;时,Typescript编译器会给出&#39;Unresolved variable or type&#39; - Typescript compiler gives 'Unresolved variable or type' when I use a 'Token' in Angular2 provider() aliasing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM