Skip to content

[no-non-null-asserted-optional-chain] false negative for foo1?.bar!.baz #2412

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
3 tasks done
xcatliu opened this issue Aug 21, 2020 · 1 comment
Closed
3 tasks done
Labels
documentation Documentation ("docs") that needs adding/updating package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@xcatliu
Copy link

xcatliu commented Aug 21, 2020

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Repro

{
  "rules": {
    "@typescript-eslint/no-non-null-asserted-optional-chain": "error"
  }
}
let foo: { bar: any } | undefined;

foo?.bar!.baz;

Expected Result

As the docs described, it should report an error.

Actual Result

No error.

image

Additional Info

foo?.bar!; report an error as expected.

image
image

Versions

package version
@typescript-eslint/eslint-plugin 3.9.1
@typescript-eslint/parser 3.9.1
TypeScript 4.0.2
ESLint 7.7.0
node 12.16.1
@xcatliu xcatliu added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Aug 21, 2020
@bradzacher
Copy link
Member

The docs are out of date. TS3.9 changed how optional chaining works.

Previously foo?.bar!.baz meant (foo?.bar).baz - which means "assume the result of foo?.bar is non-null.
As of TS3.9, foo?.bar!.baz now just applies the non-null assertion to the property itself.

So it's no longer bad practice to do this.

It is however still bad practice to do foo?.bar.baz!, because that applies the non-null assertion to the entire chain.

@bradzacher bradzacher added documentation Documentation ("docs") that needs adding/updating and removed triage Waiting for team members to take a look labels Aug 21, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
documentation Documentation ("docs") that needs adding/updating package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

2 participants