Skip to main content

js/preferOptionalChaining

MISSING DOCUMENTATION

Examples

Invalid

let foo = {};
foo && foo.bar;

 filename.ts:2 lint/js/preferOptionalChaining  FIXABLE  ━━━━━━━━━━━━━━━━━━━

  Prefer optional chaining to manual checks.

    1let foo = {};
  > 2foo && foo.bar;
     ^^^^^^^^^^^^^^

  Safe fix

  - foo·&&·foo.bar
  + foo?.bar


let foo = {};
foo !== undefined && foo.bar;

 filename.ts:2 lint/js/preferOptionalChaining  FIXABLE  ━━━━━━━━━━━━━━━━━━━

  Prefer optional chaining to manual checks.

    1let foo = {};
  > 2foo !== undefined && foo.bar;
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  Safe fix

  - foo·!==·undefined·&&·foo.bar
  + foo?.bar


let foo = {};
foo !== null && foo.bar;

 filename.ts:2 lint/js/preferOptionalChaining  FIXABLE  ━━━━━━━━━━━━━━━━━━━

  Prefer optional chaining to manual checks.

    1let foo = {};
  > 2foo !== null && foo.bar;
     ^^^^^^^^^^^^^^^^^^^^^^^

  Safe fix

  - foo·!==·null·&&·foo.bar
  + foo?.bar


let foo = {};
foo != undefined && foo.bar;

 filename.ts:2 lint/js/preferOptionalChaining  FIXABLE  ━━━━━━━━━━━━━━━━━━━

  Prefer optional chaining to manual checks.

    1let foo = {};
  > 2foo != undefined && foo.bar;
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^

  Safe fix

  - foo·!=·undefined·&&·foo.bar
  + foo?.bar


let foo = {};
foo != null && foo.bar;

 filename.ts:2 lint/js/preferOptionalChaining  FIXABLE  ━━━━━━━━━━━━━━━━━━━

  Prefer optional chaining to manual checks.

    1let foo = {};
  > 2foo != null && foo.bar;
     ^^^^^^^^^^^^^^^^^^^^^^

  Safe fix

  - foo·!=·null·&&·foo.bar
  + foo?.bar


let foo = {};
foo != null && foo.bar === "baz";

 filename.ts:2 lint/js/preferOptionalChaining  FIXABLE  ━━━━━━━━━━━━━━━━━━━

  Prefer optional chaining to manual checks.

    1let foo = {};
  > 2foo != null && foo.bar === "baz";
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  Safe fix

  - foo·!=·null·&&·foo.bar·===·"baz"
  + foo?.bar·===·"baz"


let foo = {};
undefined !== foo && foo.bar;

 filename.ts:2 lint/js/preferOptionalChaining  FIXABLE  ━━━━━━━━━━━━━━━━━━━

  Prefer optional chaining to manual checks.

    1let foo = {};
  > 2undefined !== foo && foo.bar;
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  Safe fix

  - undefined·!==·foo·&&·foo.bar
  + foo?.bar


let foo = {};
null !== foo && foo.bar;

 filename.ts:2 lint/js/preferOptionalChaining  FIXABLE  ━━━━━━━━━━━━━━━━━━━

  Prefer optional chaining to manual checks.

    1let foo = {};
  > 2null !== foo && foo.bar;
     ^^^^^^^^^^^^^^^^^^^^^^^

  Safe fix

  - null·!==·foo·&&·foo.bar
  + foo?.bar


let foo = {};
undefined != foo && foo.bar;

 filename.ts:2 lint/js/preferOptionalChaining  FIXABLE  ━━━━━━━━━━━━━━━━━━━

  Prefer optional chaining to manual checks.

    1let foo = {};
  > 2undefined != foo && foo.bar;
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^

  Safe fix

  - undefined·!=·foo·&&·foo.bar
  + foo?.bar


let foo = {};
null != foo && foo.bar;

 filename.ts:2 lint/js/preferOptionalChaining  FIXABLE  ━━━━━━━━━━━━━━━━━━━

  Prefer optional chaining to manual checks.

    1let foo = {};
  > 2null != foo && foo.bar;
     ^^^^^^^^^^^^^^^^^^^^^^

  Safe fix

  - null·!=·foo·&&·foo.bar
  + foo?.bar


let foo = {};
foo ? foo.bar.baz : undefined

 filename.ts:2 lint/js/preferOptionalChaining  FIXABLE  ━━━━━━━━━━━━━━━━━━━

  Prefer optional chaining to manual checks.

    1let foo = {};
  > 2foo ? foo.bar.baz : undefined
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  Safe fix

  - foo·?·foo.bar.baz·:·undefined
  + foo?.bar.baz


let foo = {};
if (foo != undefined && foo.bar != null) {
	foo.bar('baz');
}

 filename.ts:2:4 lint/js/preferOptionalChaining  FIXABLE  ━━━━━━━━━━━━━━━━━

  Prefer optional chaining to manual checks.

    1let foo = {};
  > 2if (foo != undefined && foo.bar != null) {
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    3  foo.bar('baz');
    4}

  Safe fix

  - foo·!=·undefined·&&·foo.bar·!=·null
  + foo?.bar·!=·null

Valid

let foo = {};
foo?.bar;
let foo = {},bar = "";
foo && bar.foo;
let foo = {},bar = "";
bar in foo && foo.bar;
let foo = {};
foo?.[bar];
let foo = {};
foo ? foo.bar.baz : null;
let foo = {};
foo ? foo.bar.baz : "anything else";