Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@mdm317
Copy link
Contributor

@mdm317 mdm317 commented Oct 16, 2025

PR Checklist

When applying optional chaining, if the transformation has the potential to alter the evaluation result,
the previous operand should not be included, even when it appears useless.

Conversely, if the transformation is guaranteed to preserve the result,
the previous operand should be included in the chain, even when it’s useless.

Not convert to optional-chain

declare const a: { b?: string };

a && a.b != "4"
a == null || a.b === "foo"
...more cases

Conver to optional-chain

declare const a: { b?: string };

a && a.b != null
a && a.b != undefined
a && a.b  !== undefined
...more cases

see Algorithm

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @mdm317!

typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community.

The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately.

Thanks again!


🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint.

@netlify
Copy link

netlify bot commented Oct 16, 2025

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit fd98e65
🔍 Latest deploy log https://app.netlify.com/projects/typescript-eslint/deploys/68f27ce7021e1e0008229b40
😎 Deploy Preview https://deploy-preview-11702--typescript-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 96 (🟢 up 1 from production)
Accessibility: 97 (no change from production)
Best Practices: 100 (no change from production)
SEO: 92 (no change from production)
PWA: 80 (no change from production)
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify project configuration.

@nx-cloud
Copy link

nx-cloud bot commented Oct 16, 2025

View your CI Pipeline Execution ↗ for commit fd98e65

Command Status Duration Result
nx test eslint-plugin --coverage=false ✅ Succeeded 5m 16s View ↗
nx run-many -t lint ✅ Succeeded 3m 14s View ↗
nx run-many -t typecheck ✅ Succeeded 2m 2s View ↗
nx run generate-configs ✅ Succeeded 9s View ↗
nx test eslint-plugin-internal --coverage=false ✅ Succeeded 4s View ↗
nx run types:build ✅ Succeeded 4s View ↗
nx test typescript-estree --coverage=false ✅ Succeeded 2s View ↗
nx run integration-tests:test ✅ Succeeded 4s View ↗
Additional runs (29) ✅ Succeeded ... View ↗

☁️ Nx Cloud last updated this comment at 2025-10-17 17:41:34 UTC

@mdm317 mdm317 changed the title fix(eslint-plugin): [prefer-optional-chain] remove unnecessary operator check fix(eslint-plugin): [prefer-optional-chain] skip optional chaining when it could change the result Oct 16, 2025
@codecov
Copy link

codecov bot commented Oct 16, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.66%. Comparing base (3f5fbf6) to head (fd98e65).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #11702      +/-   ##
==========================================
- Coverage   90.67%   90.66%   -0.01%     
==========================================
  Files         518      518              
  Lines       52466    52415      -51     
  Branches     8694     8679      -15     
==========================================
- Hits        47571    47520      -51     
  Misses       4881     4881              
  Partials       14       14              
Flag Coverage Δ
unittest 90.66% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
.../rules/prefer-optional-chain-utils/analyzeChain.ts 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mdm317 mdm317 marked this pull request as ready for review October 16, 2025 16:39
`,
`
declare const foo: { bar: number };
foo != null && foo.bar !== x;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like x is undeclared here, so this is an error type. Might be good to make it explicit?

'foo != null && foo.bar != x;',
`
declare const foo: { bar: number };
foo && foo.bar == x;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's declare x with a type here? Unless we're intentionally testing undeclared variables?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn’t declare it because I wanted to test that it doesn’t convert to an optional chain when we don’t know x’s type.
But thinking about what you said, testing with an error type feels odd and doesn’t clearly show the test’s purpose.

How about declaring x with the type any instead?

Copy link
Member

@kirkwaiblinger kirkwaiblinger Oct 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think both are potentially valid tests! But I would just make the intention explicit like:

declare const foo: { bar: number };
declare const anee: any;
foo && foo.bar == anee;
// intentionally tests that an undeclared variable doesn't trigger an autofix
declare const foo: { bar: number };
foo && foo.bar == undeclaredVar;

Copy link
Member

@kirkwaiblinger kirkwaiblinger Oct 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like there's a minor bug somewhere with:

declare const foo: { bar: number | null } | undefined;
if (foo === undefined || foo.bar === null) { // suggests to foo?.bar === null, which is not equivalent.
} else {
  foo.bar.toExponential(); // after accepting fix, this crashes if `foo` is `undefined`.
}

I guess this should either not report or it could maybe try to use foo?.bar == null? But this seems kind of bad since it is impossible to work with for an === nullishValue-prefering user

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (
includesType(parserServices, operand.comparedName, ts.TypeFlags.Null)
) {
// we know the next operand is not a `null` check and that this
// operand includes `null` - which means that making this an
// optional chain would change the runtime behavior of the expression
return null;
}

I believe this bug didn’t originate from this PR — it seems to have existed beforehand.
Since I wasn’t the original author, it might take some time to fix it properly.

If possible, we could apply a quick hotfix for the current issue first and then open a new issue to track it, but it’s just a suggestion — feel free to decide as you think best.

In any case, I’ll start digging into it now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fine if we defer it!

Just out of curiosity, do you mean to say it didn't originate in this PR, ie #11702, or in #11533? (either way we can defer it just good to know)

Copy link
Member

@kirkwaiblinger kirkwaiblinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it's on the right track, but minor touchups and a question about one bug that I saw.

Thanks for looking at this so quickly! ❤️

@kirkwaiblinger kirkwaiblinger added the 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge label Oct 17, 2025
@bradzacher bradzacher merged commit 698e7a8 into typescript-eslint:main Oct 19, 2025
67 checks passed
renovate bot added a commit to andrei-picus-tink/auto-renovate that referenced this pull request Oct 20, 2025
| datasource | package                          | from   | to     |
| ---------- | -------------------------------- | ------ | ------ |
| npm        | @typescript-eslint/eslint-plugin | 8.46.1 | 8.46.2 |
| npm        | @typescript-eslint/parser        | 8.46.1 | 8.46.2 |


## [v8.46.2](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8462-2025-10-20)

##### 🩹 Fixes

- **eslint-plugin:** \[prefer-optional-chain] skip optional chaining when it could change the result ([#11702](typescript-eslint/typescript-eslint#11702))

##### ❤️ Thank You

- mdm317

You can read about our [versioning strategy](https://typescript-eslint.io/users/versioning) and [releases](https://typescript-eslint.io/users/releases) on our website.
renovate bot added a commit to andrei-picus-tink/auto-renovate that referenced this pull request Oct 23, 2025
| datasource | package                          | from   | to     |
| ---------- | -------------------------------- | ------ | ------ |
| npm        | @typescript-eslint/eslint-plugin | 8.46.1 | 8.46.2 |
| npm        | @typescript-eslint/parser        | 8.46.1 | 8.46.2 |


## [v8.46.2](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8462-2025-10-20)

##### 🩹 Fixes

- **eslint-plugin:** \[prefer-optional-chain] skip optional chaining when it could change the result ([#11702](typescript-eslint/typescript-eslint#11702))

##### ❤️ Thank You

- mdm317

You can read about our [versioning strategy](https://typescript-eslint.io/users/versioning) and [releases](https://typescript-eslint.io/users/releases) on our website.
renovate bot added a commit to andrei-picus-tink/auto-renovate that referenced this pull request Oct 26, 2025
| datasource | package                          | from   | to     |
| ---------- | -------------------------------- | ------ | ------ |
| npm        | @typescript-eslint/eslint-plugin | 8.46.1 | 8.46.2 |
| npm        | @typescript-eslint/parser        | 8.46.1 | 8.46.2 |


## [v8.46.2](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8462-2025-10-20)

##### 🩹 Fixes

- **eslint-plugin:** \[prefer-optional-chain] skip optional chaining when it could change the result ([#11702](typescript-eslint/typescript-eslint#11702))

##### ❤️ Thank You

- mdm317

You can read about our [versioning strategy](https://typescript-eslint.io/users/versioning) and [releases](https://typescript-eslint.io/users/releases) on our website.
renovate bot added a commit to andrei-picus-tink/auto-renovate that referenced this pull request Nov 1, 2025
| datasource | package                          | from   | to     |
| ---------- | -------------------------------- | ------ | ------ |
| npm        | @typescript-eslint/eslint-plugin | 8.46.1 | 8.46.2 |
| npm        | @typescript-eslint/parser        | 8.46.1 | 8.46.2 |


## [v8.46.2](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8462-2025-10-20)

##### 🩹 Fixes

- **eslint-plugin:** \[prefer-optional-chain] skip optional chaining when it could change the result ([#11702](typescript-eslint/typescript-eslint#11702))

##### ❤️ Thank You

- mdm317

You can read about our [versioning strategy](https://typescript-eslint.io/users/versioning) and [releases](https://typescript-eslint.io/users/releases) on our website.
renovate bot added a commit to andrei-picus-tink/auto-renovate that referenced this pull request Nov 3, 2025
| datasource | package                          | from   | to     |
| ---------- | -------------------------------- | ------ | ------ |
| npm        | @typescript-eslint/eslint-plugin | 8.46.1 | 8.46.2 |
| npm        | @typescript-eslint/parser        | 8.46.1 | 8.46.2 |


## [v8.46.2](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8462-2025-10-20)

##### 🩹 Fixes

- **eslint-plugin:** \[prefer-optional-chain] skip optional chaining when it could change the result ([#11702](typescript-eslint/typescript-eslint#11702))

##### ❤️ Thank You

- mdm317

You can read about our [versioning strategy](https://typescript-eslint.io/users/versioning) and [releases](https://typescript-eslint.io/users/releases) on our website.
renovate bot added a commit to andrei-picus-tink/auto-renovate that referenced this pull request Nov 8, 2025
| datasource | package                          | from   | to     |
| ---------- | -------------------------------- | ------ | ------ |
| npm        | @typescript-eslint/eslint-plugin | 8.46.1 | 8.46.3 |
| npm        | @typescript-eslint/parser        | 8.46.1 | 8.46.3 |


## [v8.46.3](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8463-2025-11-03)

##### 🩹 Fixes

- **eslint-plugin:** \[no-duplicate-enum-values] support signed numbers ([#11722](typescript-eslint/typescript-eslint#11722), [#11723](typescript-eslint/typescript-eslint#11723))
- **eslint-plugin:** \[no-misused-promises] expand union type to retrieve target property ([#11706](typescript-eslint/typescript-eslint#11706))

##### ❤️ Thank You

- Evgeny Stepanovych [@undsoft](https://github.com/undsoft)
- tao

You can read about our [versioning strategy](https://typescript-eslint.io/users/versioning) and [releases](https://typescript-eslint.io/users/releases) on our website.


## [v8.46.2](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8462-2025-10-20)

##### 🩹 Fixes

- **eslint-plugin:** \[prefer-optional-chain] skip optional chaining when it could change the result ([#11702](typescript-eslint/typescript-eslint#11702))

##### ❤️ Thank You

- mdm317

You can read about our [versioning strategy](https://typescript-eslint.io/users/versioning) and [releases](https://typescript-eslint.io/users/releases) on our website.
renovate bot added a commit to andrei-picus-tink/auto-renovate that referenced this pull request Nov 9, 2025
| datasource | package                          | from   | to     |
| ---------- | -------------------------------- | ------ | ------ |
| npm        | @typescript-eslint/eslint-plugin | 8.46.1 | 8.46.3 |
| npm        | @typescript-eslint/parser        | 8.46.1 | 8.46.3 |


## [v8.46.3](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8463-2025-11-03)

##### 🩹 Fixes

- **eslint-plugin:** \[no-duplicate-enum-values] support signed numbers ([#11722](typescript-eslint/typescript-eslint#11722), [#11723](typescript-eslint/typescript-eslint#11723))
- **eslint-plugin:** \[no-misused-promises] expand union type to retrieve target property ([#11706](typescript-eslint/typescript-eslint#11706))

##### ❤️ Thank You

- Evgeny Stepanovych [@undsoft](https://github.com/undsoft)
- tao

You can read about our [versioning strategy](https://typescript-eslint.io/users/versioning) and [releases](https://typescript-eslint.io/users/releases) on our website.


## [v8.46.2](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8462-2025-10-20)

##### 🩹 Fixes

- **eslint-plugin:** \[prefer-optional-chain] skip optional chaining when it could change the result ([#11702](typescript-eslint/typescript-eslint#11702))

##### ❤️ Thank You

- mdm317

You can read about our [versioning strategy](https://typescript-eslint.io/users/versioning) and [releases](https://typescript-eslint.io/users/releases) on our website.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: @typescript-eslint/prefer-optional-chain flags situations where an optional chain would change behavior

3 participants