1

I use some bitwise operators in my JS (especially left and right shift, such as bytes[i + 1] << 8). However, TypeScript generates warnings and I see the value in avoiding this to minimize the risk of serious typos.

Furthermore, regardless of linting errors, bitwise operators are unsafe on numbers over 2^32

What is a type-safe alternative to bitwise operators such as <<, >>, &, and |?

slifty
  • 13,062
  • 13
  • 71
  • 109
Robert T
  • 173
  • 2
  • 10
  • 5
    You could disable the [no-bitwise](https://palantir.github.io/tslint/rules/no-bitwise/) rule if you want to write code like that. This isn't a type safety problem, it's coming from your *linter*. – jonrsharpe Aug 27 '20 at 15:44
  • I know how to disable the linter, but like the value of this check (to avoid bad typos) outside of a very few functions where I do use bitwise operators. – Robert T Aug 27 '20 at 15:55
  • Then are you really asking how to disable the rule in a specific scope, rather than (per the title and content of the question) what an alternative implementation would be? That's covered in [the docs](https://palantir.github.io/tslint/usage/rule-flags/). – jonrsharpe Aug 27 '20 at 15:55
  • 3
    @RobertT Then only disable the rule for those lines but keep it on otherwise. – VLAZ Aug 27 '20 at 15:56
  • /* tslint:disable:no-bitwise */ and /* tslint:enable:no-bitwise */ is perfect to ignore the specific function where I'm using them, thank you all! – Robert T Aug 28 '20 at 16:44

0 Answers0