typescript tslint 一行规则错位“其他”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35261282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
tslint one line rule misplaced 'else'
提问by sreginogemoh
I have such config
in tslint.json
for one line rule
我有这样config
的tslint.json
对one line rule
one-line": [true,
"check-open-brace",
"check-catch",
"check-else",
"check-whitespace"
],
When I have code lines like that:
当我有这样的代码行时:
if(SomethingTrue) { next("a"); }
else { next("b"); }
I've got warning:
我有警告:
(one-line) file.ts[17, 9]: misplaced 'else'
Why that is happens?
Is it bad practice to have one line else
?
为什么会这样?拥有一个是不好的做法line else
吗?
采纳答案by basarat
You have :
你有 :
else { next("b"); }
Else must be one one line. So:
否则必须一一对应。所以:
else {
next("b");
}
Is it bad practice to have one line else?
拥有一条其他线路是不好的做法吗?
Just easier to read. Its a styleguide for consistency.
只是更容易阅读。它是一致性的风格指南。
回答by user7823874
if (condition is true) {
// do something;
}
else {
// do something else;
}
Notice that else
comes next to }
注意else
旁边的}
if (condition is true) {
// do something;
} else {
// do something else;
}
回答by zejuel
According to the tslint docsthe problem is that when "check-else"
is specified under one-line
the else must be on the same line as the closing brace for your if.
根据tslint 文档,问题是在 else"check-else"
下指定的when必须与if 的右one-line
大括号在同一行。
So in your case instead of:
所以在你的情况下,而不是:
if(SomethingTrue) { next("a"); }
else { next("b"); }
Use this format:
使用这种格式:
if(SomethingTrue) { next("a"); } else { next("b"); }
回答by SReddy
if (condition) {
// Your Code
} else {
// Your Code
}
End of If
and start of else
should be on the same line.
End ofIf
和 start ofelse
应该在同一行。