Skip to content

Commit

Permalink
docs(eslint-plugin): [no-non-null-asserted-optional-chain] correct do…
Browse files Browse the repository at this point in the history
…cs for 3.9 functionality

Closes #2412
  • Loading branch information
bradzacher committed Aug 21, 2020
1 parent fde89d4 commit fd2070c
Showing 1 changed file with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@
Optional chain expressions are designed to return `undefined` if the optional property is nullish.
Using non-null assertions after an optional chain expression is wrong, and introduces a serious type safety hole into your code.



Examples of **incorrect** code for this rule:

```ts
/* eslint @typescript-eslint/no-non-null-asserted-optional-chain: "error" */

foo?.bar!;
foo?.bar!.baz;
foo?.bar()!;

// Prior to TS3.9, foo?.bar!.baz meant (foo?.bar).baz - i.e. the non-null assertion is applied to the entire chain so far.
// For TS3.9 and greater, the non-null assertion is only applied to the property itself, so it's safe.
// The following is incorrect code if you're using less than TS3.9
foo?.bar!.baz;
foo?.bar!();
foo?.bar!().baz;
```
Expand All @@ -27,6 +33,11 @@ foo?.bar;
foo?.bar();
foo?.bar();
foo?.bar().baz;

// The following is correct code if you're using TS3.9 or greater
foo?.bar!.baz;
foo?.bar!();
foo?.bar!().baz;
```

## When Not To Use It
Expand Down

0 comments on commit fd2070c

Please sign in to comment.