typescript - tslint 错位 'else'(单行)

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

我正在为 VS 代码使用 TSLint 扩展。我收到一堆 [tslint] misplaced 'else' (one-line) 警告。例如,在下面的代码示例中,linter 会在 else 单词下划线并给出相同的警告:

代码:

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

这段代码有什么问题?

来自tslint one-line rule documentation

Rule: one-line

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

您启用了名为 one-line 的 tslint 规则,并指定了一个名为 check-else 的令牌。这告诉 tslint 警告每个 else 与前一个结束大括号不在同一行的语句。如果将 else 语句向上移动到与 if 语句的结束花括号相同的行,则应满足 tslint 规则。

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

始终确保结尾 if 花括号和 else 语句在同一行。

这是根据 TSLint 单行规则文档。