Skip to content

[prefer-optional-chain] Ignore assignment patterns #4892

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
NullDivision opened this issue May 2, 2022 · 2 comments
Closed
3 tasks done

[prefer-optional-chain] Ignore assignment patterns #4892

NullDivision opened this issue May 2, 2022 · 2 comments
Labels
package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin wontfix This will not be worked on

Comments

@NullDivision
Copy link

  • 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/prefer-optional-chain": "warn"
  }
}
// your repro code case
const a: { b: string | null } = { b: null };

const x = a.b && a.b?.includes('test');

Expected Result

The code is considered valid.

Actual Result

ESLint tells me Prefer using an optional chain expression instead, as it's more concise and easier to read..

Additional Info

Versions

package version
@typescript-eslint/eslint-plugin 5.21.0
@typescript-eslint/parser 5.21.0
TypeScript 4.6.4
ESLint 8.14.0
node 16.15.0

In the above example the output of optional chaining would not be equivalent as a.b would short- circuit with a null value and return null. I'm sure this falls under the warning mentioned in the docs about edge cases but a nicer solution would be to either:

a. ignore optional chaining warnings for assignments only (using an option for the rule)
b. use type information (mentioned in #4820) to determine if the resulting value might be null

@NullDivision NullDivision added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels May 2, 2022
@bradzacher
Copy link
Member

In the above example the output of optional chaining would not be equivalent as a.b would short- circuit with a null value and return null.

a.b && a.b.thing will return null if a.b is null
a.b?.thing will return undefined if a.b is null.

Either way they both short-circuit. They are mostly equivalent - as long as your code doesn't differentiate between null and undefined.


I don't think an option here has wide-enough applicability to add to the rule. Feel free to fork it if thus is important to you!

@bradzacher bradzacher added wontfix This will not be worked on and removed triage Waiting for team members to take a look labels May 2, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 2, 2022
@JoshuaKGoldberg
Copy link
Member

Note: per #4820 we're going to add type checking to the rule so that it can understand the difference between a null and undefined a.b in a.b && a.b.thing (Brad's comment just before this one).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants