Why are bitwise operators not allowed in tslint?
Asked Answered
H

3

43

We can't use bitwise operators in templates, but why are they not allowed by tslint within TypeScript code?

"no-bitwise": true,
Humperdinck answered 3/12, 2017 at 20:14 Comment(0)
E
27

Linters exist for multiple reasons: to help maintain consistent, clean and readable code, catch developer mistakes (e.g. unreachable code or unused variables) and to warn you about potentially bad practices even though they may technically be allowed.

As mentioned in the TSLint documentation

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.

Since these types of typos are so much more common than actual valid uses of bitwise operators, TSLint forbids them by default.

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

Equilibrist answered 23/6, 2020 at 17:20 Comment(4)
It's a shame there isn't a lint rule that says "you forget to re-enable the rule that you disabled".Gerous
@Gerous haha, yes it is, looks like a joke. Anyway you can always golintMenorca
Is there another trick to get this to work? Adding the comments as no effect for me.Snippet
@BerryBlue is it just this rule specifically that's not working for you, or none of the rules are workingEquilibrist
N
18

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.

https://palantir.github.io/tslint/rules/no-bitwise/

Nakano answered 3/4, 2018 at 21:38 Comment(2)
@Humperdinck hahaha this made me laugh very hard for a good 2 minutes!Perspiration
What about for fallbacks? | is better than ||, because it doesn't fall back on values like 0.Jarietta
B
4

If you look at the Docs

"Bitwise operators are very rare in JavaScript programs"

anyhow you can disable the bitwise option to stop the warnings.

Blandish answered 3/12, 2017 at 20:16 Comment(2)
This is JSHint not an explanation from Angular perspective. And even if something is rare, it should not be forbidden?Humperdinck
Angular does not have anything to do with above, its Typescript , also check here github.com/palantir/tslint/issues/18Blandish

© 2022 - 2024 — McMap. All rights reserved.