Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typescript - tslint misplaced 'else' (one-line)

I'm using TSLint extension for VS Code. I got bunch of [tslint] misplaced 'else' (one-line) warnings. For example, at the below code sample, linter underlines else word and gives same warning:

Code:

if (user.isAdmin) {
    this.uiRouter.stateService.go('app.admin');
}
else {
    this.sharedVariables.user = user;
    this.navigateByUserType(user);
}

What is the problem with this code?

like image 557
Emre Avatar asked Mar 17 '17 06:03

Emre


1 Answers

From the tslint one-line rule documentation

Rule: one-line

Requires the specified tokens to be on the same line as the expression preceding them.

You have a tslint rule enabled called one-line with a token specified called check-else. This tells tslint to warn for every else statement that isn't on the same line as the previous ending curly brace. If you move up the else statement to the same line as the ending curly brace of the if statement the tslint rule should be satisfied.

if (user.isAdmin) {
    this.uiRouter.stateService.go('app.admin');
} else {
    this.sharedVariables.user = user;
    this.navigateByUserType(user);
}
like image 140
jontem Avatar answered Nov 05 '22 02:11

jontem