5

Sadly, this valid code is considered negligent by the default setting of TSLint:

export const NO_FLAG: number = 0x0000;
export const DESTROY_FLAG: number = 0x0001;
export const NULL_FLAG: number = 0x0100;
export const START_FLAG: number = 0x0200;
export const STOP_FLAG: number = 0x0400;

export function getPackedFlags(destroy: boolean,
                               nullThing: boolean,
                               start: boolean,
                               stop: boolean): number {
    const bitFlags: number = ((destroy) ? DESTROY_FLAG: NO_FLAG) |
                            ((nullThing) ? NULL_FLAG: NO_FLAG) |
                            ((start) ? START_FLAG: NO_FLAG) |
                            ((stop) ? STOP_FLAG: NO_FLAG);
    return bitFlags;
}

Doing the above produces this kind of output:

tslint --project ./tsconfig.json --format verbose --force

ERROR: (no-bitwise) C:/git/my-stuff/src/index.ts[393, 34]: Forbidden bitwise operation
ERROR: (no-bitwise) C:/git/my-stuff/src/index.ts[393, 34]: Forbidden bitwise operation
ERROR: (no-bitwise) C:/git/my-stuff/src/index.ts[393, 34]: Forbidden bitwise operation

The authors of TSLint have a strategy of setting this as default error. However, the Typescript compiler and proper programming usage dictates this is correct use of the | operator. In cases, where you intend to use bitwise, calling it an error is just plain silly.

I don't know how to turn off this linting issue on a case-by-case basis, but keep the global setting unchanged.

Kim Gentes
  • 1,496
  • 1
  • 18
  • 38
  • My observation is that rules in TSLint don't use type information unless it's absolutely necessary. That's the case for the [`no-bitwise`](https://github.com/palantir/tslint/blob/5.10.0/src/rules/noBitwiseRule.ts#L40) rule. It's rule that could be enforced differently if type information were required, but that would impose a performance cost and would also make a `tsconfig.json` file mandatory. – cartant Jun 13 '18 at 23:29

1 Answers1

5

Fortunately, you can disable this on a single line basis, as follows:

export function getPackedFlags(destroy: boolean,
                               nullThing: boolean,
                               start: boolean,
                               stop: boolean): number {
    // tslint:disable-next-line:no-bitwise
    const bitFlags: number = ((destroy) ? DESTROY_FLAG: NO_FLAG) |
                            ((nullThing) ? NULL_FLAG: NO_FLAG) |
                            ((start) ? START_FLAG: NO_FLAG) |
                            ((stop) ? STOP_FLAG: NO_FLAG);
    return bitFlags;
}
Kim Gentes
  • 1,496
  • 1
  • 18
  • 38
  • 1
    thank you very much. I tried to use 【@ts-ignore】, but it didn't work. But using your 【tslint:disable-next-line】, it can take effect. – Lancer.Yan Aug 21 '20 at 10:33