简体   繁体   English

方法中一行的tslint错误

[英]tslint errors for a line in a method

  • I am new to tslint and typescript. 我是tslint和打字稿的新手。
  • I am trying to fix this error. 我正在尝试解决此错误。 Unnecessary local variable: stackThird 不必要的局部变量:stackThird
  • Can you tell me how to fix it. 你能告诉我如何解决它。
  • I did some research but not able to find solutions. 我做了一些研究,但找不到解决方案。
  • It's pointing to this line let stackThird = stackSecond + "/" + stackFirst.stackTags() + "/" + stackFirst.stackFour(); 它指向这一行,让stackThird = stackSecond +“ /” + stackFirst.stackTags()+“ /” + stackFirst.stackFour(); // +" "+Time; // +“” + Time;

  • providing code below. 在下面提供代码。

  • I even looked at this link and tried but not able to proceed 我什至查看了此链接并尝试但无法继续

https://github.com/Microsoft/tslint-microsoft-contrib https://github.com/Microsoft/tslint-microsoft-contrib

Unnecessary local variable: stackThird 不必要的局部变量:stackThird

 public stackTags(): any {
    let stackFirst = new Date();
    let stackSecond = stackFirst.stackFive();
    stackSecond++;
    let stackThird = stackSecond + "/" + stackFirst.stackTags() + "/" + stackFirst.stackFour(); // +" "+Time;
    return stackThird;
  }

Change 更改

let stackThird = stackSecond + "/" + stackFirst.stackTags() + "/" + stackFirst.stackFour();
return stackThird;`

to

return stackSecond + "/" + stackFirst.stackTags() + "/" + stackFirst.stackFour();

It is complaining because you are creating an unnecessary variable(stackThird). 抱怨是因为您正在创建不必要的变量(stackThird)。
Since you aren't doing anything with it after declaring/assigning it, it is complaining because you should just return that value from the method instead of assigning it to a variable and then returning the variable. 由于在声明/赋值之后对它不做任何事情,所以它在抱怨,因为您应该只从方法中返回该值,而不是将其分配给变量,然后返回该变量。

The rule no-unnecessary-local-variable is this one: 规则no-unnecessary-local-variable就是这个规则:

Do not declare a variable only to return it from the function on the next line. 不要声明变量只是要从下一行的函数中返回它。 It is always less code to simply return the expression that initializes the variable. 仅返回初始化变量的表达式总是很少的代码。

It is not part of the standard tslint, but come from tslint-microsoft-contrib , which are a set of rules even more strict. 它不是标准tslint的一部分,但来自tslint-microsoft-contrib ,后者是一组更加严格的规则。

You can disable this rule if you don't like it (personnaly, that's what I've done in my projects): 如果您不喜欢此规则,可以将其禁用(过时的,这就是我在项目中所做的事情):

// tslint.json
{
    "rulesDirectory": [
        "node_modules/tslint-microsoft-contrib"
    ],
    "rules": {
        "no-unnecessary-local-variable": false
    }
}

Or you can fix it by returning the computed result without using the variable: 或者,您可以不使用变量而通过返回计算结果来修复它:

return stackSecond + "/" + stackFirst.stackTags() + "/" + stackFirst.stackFour();

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

相关问题 打字稿 - tslint 放错了“else”(一行) - typescript - tslint misplaced 'else' (one-line) Angular 2-在我的node_modules文件夹中收到TSLint错误 - Angular 2 - Receiving TSLint errors in my node_modules folder 如何在 eslint/tslint 中配置忽略单行语句? - How to config ignore single line statement in eslint/tslint? tslint - 最后一行缺少尾随逗号(尾随逗号) - tslint - Missing trailing comma (trailing-comma) on the last line Angular2项目中的TSLint错误:无法将类型&#39;Subscription&#39;分配给类型&#39;FirebaseListObservable <any[]> “ - TSLint errors in Angular2 project: Type 'Subscription' is not assignable to type 'FirebaseListObservable<any[]>' 如何在JavaScript的一行中无错误地调用可能不存在的方法? - How can I call a possibly-non-existent method, without errors, in one line in JavaScript? TSLint 抛出 'error TS2459: Module '"@azure/core-tracing"' 在本地声明 'Span',但未导出。' 和其他错误 - TSLint is throwing 'error TS2459: Module '"@azure/core-tracing"' declares 'Span' locally, but it is not exported.' and other errors 在Codemirror中显示行错误? - Show line errors in codemirror? JavaScript中charAt方法出现错误 - errors with charAt method in javascript typescript中调用方法出错 - Errors in call method in typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM