fix(no-unnecessary-condition): handle IndexedAccess types in ?? and ??= paths#784
Merged
camc314 merged 2 commits intooxc-project:mainfrom Mar 10, 2026
Merged
fix(no-unnecessary-condition): handle IndexedAccess types in ?? and ??= paths#784camc314 merged 2 commits intooxc-project:mainfrom
camc314 merged 2 commits intooxc-project:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes a false positive in the no-unnecessary-condition rule when analyzing nullish coalescing (??) and nullish coalescing assignment (??=) where the LHS type is an unresolved indexed access involving generics (e.g., Partial<Record<R, T[]>>[R]).
Changes:
- Add a shared
checkIndexedAccessNullishhelper to resolve indexed access types viagetBaseConstraintOfTypeand skip/report conservatively. - Apply the indexed-access nullish handling to both
??and??=code paths. - Add valid test coverage for
Partial<Record<R, T[]>>+ generic index under both default andnoUncheckedIndexedAccessconfigs (for??).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| internal/rules/no_unnecessary_condition/no_unnecessary_condition.go | Adds constraint-based handling for unresolved indexed access types in ??/??= to avoid false positives. |
| internal/rules/no_unnecessary_condition/no_unnecessary_condition_test.go | Adds new valid cases exercising the unresolved generic indexed-access scenario (currently for ??). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
…?= paths Indexed access types like `Partial<Record<R, T[]>>[R]` with generic type parameters cannot be fully resolved at compile time. The ?? and ??= handlers were missing this case, causing false positives when the unresolved type appeared non-nullish. Resolve the type via `getBaseConstraintOfType`; if the constraint is still unresolvable or includes nullish, skip the report conservatively. Fixes oxc-project/oxc#20149
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
no-unnecessary-conditionwhen the left side of??or??=is an unresolved indexed access type (e.g.,Partial<Record<R, T[]>>[R]whereRis a generic type parameter)getBaseConstraintOfType; if the constraint is still unresolvable or includes nullish, skip the report conservativelycheckIndexedAccessNullishclosure to avoid duplication between??and??=pathsRelated Issue in oxc: oxc-project/oxc#20149
Test plan
Partial<Record<R, T[]>>(with and withoutnoUncheckedIndexedAccess)invalid-49test (T[K]with known non-nullish constraint) still correctly reportsgo test ./internal/rules/no_unnecessary_condition/ -count=1