Skip to main content

js/noExtraBooleanCast

disallow unnecessary boolean casts

ESLint Equivalent: no-extra-boolean-cast

Examples

Invalid

if (Boolean(foo)) {}

 file.ts:1:4 lint/js/noExtraBooleanCast  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━

  Avoid redundant double-negation.

    if (Boolean(foo)) {}
        ^^^^^^^^^^^^

  It is not necessary to use double-negation when a value will already
    be coerced to a boolean.

  Safe fix

  - Boolean(foo)
  + foo


if (!!Boolean(foo)) {}

 file.ts:1:4 lint/js/noExtraBooleanCast  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━

  Avoid redundant double-negation.

    if (!!Boolean(foo)) {}
        ^^^^^^^^^^^^^^

  It is not necessary to use double-negation when a value will already
    be coerced to a boolean.

  Safe fix

  - !!Boolean(foo)
  + Boolean(foo)

 file.ts:1:6 lint/js/noExtraBooleanCast  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━

  Avoid redundant double-negation.

    if (!!Boolean(foo)) {}
          ^^^^^^^^^^^^

  It is not necessary to use double-negation when a value will already
    be coerced to a boolean.

  Safe fix

  - Boolean(foo)
  + foo


if (!Boolean(foo)) {}

 file.ts:1:5 lint/js/noExtraBooleanCast  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━

  Avoid redundant double-negation.

    if (!Boolean(foo)) {}
         ^^^^^^^^^^^^

  It is not necessary to use double-negation when a value will already
    be coerced to a boolean.

  Safe fix

  - Boolean(foo)
  + foo


while (!!foo) {}

 file.ts:1:7 lint/js/noExtraBooleanCast  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━

  Avoid redundant double-negation.

    while (!!foo) {}
           ^^^^^

  It is not necessary to use double-negation when a value will already
    be coerced to a boolean.

  Safe fix

  - !!foo
  + foo


let x = 1;
do {
	1 + 1;
} while (Boolean(x));

 file.ts:4:9 lint/js/noExtraBooleanCast  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━

  Avoid redundant double-negation.

    2do {
    3  1 + 1;
  > 4} while (Boolean(x));
              ^^^^^^^^^^

  It is not necessary to use double-negation when a value will already
    be coerced to a boolean.

  Safe fix

  - Boolean(x)
  + x


for (; !!foo; ) {}

 file.ts:1:7 lint/js/noExtraBooleanCast  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━

  Avoid redundant double-negation.

    for (; !!foo; ) {}
           ^^^^^

  It is not necessary to use double-negation when a value will already
    be coerced to a boolean.

  Safe fix

  - !!foo
  + foo


new Boolean(!!x);

 file.ts:1:12 lint/js/noExtraBooleanCast  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━

  Avoid redundant double-negation.

    new Boolean(!!x);
                ^^^

  It is not necessary to use double-negation when a value will already
    be coerced to a boolean.

  Safe fix

  - !!x
  + x


!!!x;

 file.ts:1:1 lint/js/noExtraBooleanCast  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━

  Avoid redundant double-negation.

    !!!x;
     ^^^

  It is not necessary to use double-negation when a value will already
    be coerced to a boolean.

  Safe fix

  - !!x
  + x


!Boolean(x);

 file.ts:1:1 lint/js/noExtraBooleanCast  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━

  Avoid redundant double-negation.

    !Boolean(x);
     ^^^^^^^^^^

  It is not necessary to use double-negation when a value will already
    be coerced to a boolean.

  Safe fix

  - Boolean(x)
  + x

Valid

Boolean(!x);
!x;
!!x;