Skip to content

Latest commit

 

History

History
39 lines (27 loc) · 902 Bytes

no-optional-chaining.md

File metadata and controls

39 lines (27 loc) · 902 Bytes

no-optional-chaining

This prevents the use of Optional Chaining:

const baz = obj?.foo?.bar?.baz; // 42

These will not be allowed because they are not supported in the following browsers:

  • Edge < 80
  • Safari < 13.1
  • Firefox < 72
  • Chrome < 80

What is the Fix?

If the expression is short, you can consider using a ternary operator:

// these are equivalent:
foo?.bar
foo == null ? void 0 : foo.bar;

You can also use the Logical OR operator to avoid throwing, although these can look messy:

const baz = (((obj || {}).foo || {}).bar || {}).baz

Lastly, you could consider using a utility function such as lodash' get

const baz = _.get(obj, 'foo.bar.baz')

This can be safely disabled if you intend to compile code with the @babel/plugin-transform-optional-chaining Babel plugin, or @babel/preset-env.