当在代码中进行位操作时,默认 TSLint 会提示:Forbidden bitwise operation (no-bitwise)。
但是位操作在有些场合是很有用的,比如一些标志位。因此很奇怪,为什么 TSLint 会将其禁用掉。
查询条目解析后,不禁失笑,原来如此!
https://palantir.github.io/tslint/rules/no-bitwise/
Rule: no-bitwise
Disallows bitwise operators.
Specifically, the following bitwise operators are banned:
&
,&=
,|
,|=
,^
,^=
,<<
,<<=
,>>
,>>=
,>>>
,>>>=
, and~
. This rule does not ban the use of&
and|
for intersection and union types.Rationale
Bitwise operators are often typos - for example
bool1 & bool2
instead ofbool1 && bool2
. They also can be an indicator of overly clever code which decreases maintainability.
原因只是因为容易误写,比如把 "bool1 && bool2" 误写成 "bool1 & bool2”,禁用后,使用 TSLint 会给出提示,方便确认是否有错写。
如果确实要用位操作,又不想看到警告提示,请加上注释: // tslint:disable-next-line: no-bitwise