简体   繁体   中英

tslint errors for a line in a method

  • I am new to tslint and typescript.
  • I am trying to fix this error. Unnecessary local variable: 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(); // +" "+Time;

  • providing code below.

  • I even looked at this link and tried but not able to proceed

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

Unnecessary local variable: 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).
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:

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.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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