[英]Why are bitwise operators not allowed in tslint?
我们不能在模板中使用按位运算符,但为什么 TypeScript 代码中的 tslint 不允许使用位运算符?
"no-bitwise": true,
Bitwise operators are often typos - for example bool1 & bool2 instead of bool1 && bool2.按位运算符通常是错别字 - 例如 bool1 & bool2 而不是 bool1 && bool2。 They also can be an indicator of overly clever code which decreases maintainability.它们也可能表明代码过于聪明,从而降低了可维护性。
https://palantir.github.io/tslint/rules/no-bitwise/ https://palantir.github.io/tslint/rules/no-bitwise/
Linters exist for multiple reasons: to help maintain consistent, clean and readable code, catch developer mistakes (eg unreachable code or unused variables) and to warn you about potentially bad practices even though they may technically be allowed. Linter 的存在有多种原因:帮助维护一致、干净和可读的代码,捕捉开发人员的错误(例如无法访问的代码或未使用的变量)并警告您潜在的不良做法,即使它们在技术上是允许的。
As mentioned in the TSLint documentation如TSLint 文档中所述
Bitwise operators are often typos - for example
bool1 & bool2
instead ofbool1 && bool2
.按位运算符通常是错别字 - 例如bool1 & bool2
而不是bool1 && bool2
。 They also can be an indicator of overly clever code which decreases maintainability.它们也可能表明代码过于聪明,从而降低了可维护性。
Since these types of typos are so much more common than actual valid uses of bitwise operators, TSLint forbids them by default .由于这些类型的拼写错误比按位运算符的实际有效使用更常见,TSLint默认禁止它们。
Unless you're working on an application whose sole purpose is to do bitwise operations, it's best to keep the rule enabled (because just like anyone else you are prone to making this kind of typo).除非您正在处理的应用程序的唯一目的是进行按位运算,否则最好保持启用规则(因为就像其他人一样,您很容易犯这种类型的错字)。 If however you do have a valid case to use bitwise, then disable the rule temporarily just for that line or block of code, like this:但是,如果您确实有一个有效的情况可以按位使用,则暂时仅针对该行或代码块禁用该规则,如下所示:
/* tslint:disable:no-bitwise */
const redColor = (decimalColor & 0xff0000) >> 16;
const greenColor = (decimalColor & 0x00ff00) >> 8;
const blueColor = decimalColor & 0x0000ff;
/* tslint:enable:no-bitwise */
don't forget to re-enable the rule!不要忘记重新启用规则!
or for a single line:或单行:
// tslint:disable-next-line:no-bitwise
const redColor = (decimalColor & 0xff0000) >> 16;
If using ESLint, see documentation here如果使用 ESLint,请参阅此处的文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.