Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are bitwise operators not allowed in tslint?

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

"no-bitwise": true,
like image 862
Mick Avatar asked Dec 03 '17 20:12

Mick


People also ask

Can bitwise operators Cannot be utilized on the float type?

Bitwise operations cannot be used on boolean, float, or double, or class types. They are called the bitwise operators because they are used to test, set, or shift the individual bits that make up a value.

Can bitwise operators be applied to float or double variables?

16 : Bitwise Operators (i) Bitwise operators cannot be applied to float or double. They can be applied to integers only.

Does Python support bitwise operators?

Python bitwise operators are used to perform bitwise calculations on integers. The integers are converted into binary format and then operations are performed bit by bit, hence the name bitwise operators. Python bitwise operators work on integers only and the final output is returned in the decimal format.

Which data types can be used with bitwise operators?

There are four bitwise operators in IDL: AND, NOT, OR, and XOR. For integer operands (byte, signed- and unsigned-integer, longword, and 64-bit longword data types), bitwise operators operate on each bit of the operand or operands independently.


3 Answers

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

like image 169
Sergey Avatar answered Oct 19 '22 08:10

Sergey


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/

like image 39
Grant Eagon Avatar answered Oct 19 '22 09:10

Grant Eagon


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.

like image 45
Sajeetharan Avatar answered Oct 19 '22 08:10

Sajeetharan