Skip to content

Commit

Permalink
docs: add docs for no-optional-chaining
Browse files Browse the repository at this point in the history
  • Loading branch information
keithamus committed Sep 20, 2019
1 parent d8ce1cb commit d7bc64e
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions docs/no-optional-chaining.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# no-optional-chaining

This prevents the use of Optional Chaining:

```js
const baz = obj?.foo?.bar?.baz; // 42
```
These will not be allowed because they are not supported in the following browsers:
- Edge (any version at the time of writing)
- Safari (any version at the time of writing)
- Firefox (any version at the time of writing)
- Chrome (any version at the time of writing)
## What is the Fix?
If the expression is short, you can consider using a ternary operator:
```js
// 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:
```js
const baz = (((obj || {}).foo || {}).bar || {}).baz
```
Lastly, you could consider using a utility function such as [lodash' `get`](https://lodash.com/docs/4.17.15#get)
```js
const baz = _.get(obj, 'foo.bar.baz')
```
This can be safely disabled if you intend to compile code with the `@babel/plugin-proposal-optional-chaining` Babel plugin.

0 comments on commit d7bc64e

Please sign in to comment.