Typescript TSLint 为什么默认禁止位操作?

本文探讨了TSLint为何禁止使用位操作符,如&, |, ^等,并解释了这一规则的原因是为了避免常见的误写,如将bool1&&bool2误写为bool1&bool2,同时也提到了如何在确需使用位操作时避免TSLint的警告。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当在代码中进行位操作时,默认 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 of bool1 && 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值