[英]tslint errors for a line in a method
It's pointing to this line let stackThird = stackSecond + "/" + stackFirst.stackTags() + "/" + stackFirst.stackFour(); 它指向这一行,让stackThird = stackSecond +“ /” + stackFirst.stackTags()+“ /” + stackFirst.stackFour(); // +" "+Time;
// +“” + Time;
providing code below. 在下面提供代码。
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.