fix(no-unnecessary-condition): use resolved call types for optional chains#792
Conversation
How to use the Graphite Merge QueueAdd the label 0-merge to this PR to add it to the merge queue. You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. This stack of pull requests is managed by Graphite. Learn more about stacking. |
There was a problem hiding this comment.
Pull request overview
Fixes false positives in no-unnecessary-condition for overloaded APIs (e.g., react-hook-form’s getValues(path)) by improving how the rule determines call expression types when validating optional chaining.
Changes:
- Update call-type extraction to prefer the resolved call-site type for non-optional-chained calls (better overload selection).
- Add a regression test covering an overloaded
getValues(\data.${number}`)` call followed by optional chaining.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| internal/rules/no_unnecessary_condition/no_unnecessary_condition.go | Adjusts call return type inference to use resolved call-site types when appropriate. |
| internal/rules/no_unnecessary_condition/no_unnecessary_condition_test.go | Adds a regression test for overloaded path-based access followed by ?.. |
Comments suppressed due to low confidence (1)
internal/rules/no_unnecessary_condition/no_unnecessary_condition.go:756
- In the optional-chain path (when
call.QuestionDotToken != nilorhasOptionalChain(call.Expression)is true), this still derives the return type from the callee type and then pickssignatures[0]. For overloaded APIs this can be incorrect because it ignores the resolved overload at the call site (e.g.,form?.getValues(data.0)will likely select the wrong overload and can reintroduce the false positive). Consider usingctx.TypeChecker.GetResolvedSignature(callExpr)andGetReturnTypeOfSignaturewhen available, falling back to the current union-of-functions handling only when there is no resolved signature.
call := callExpr.AsCallExpression()
if call.QuestionDotToken == nil && !hasOptionalChain(call.Expression) {
if callType := getResolvedType(callExpr); callType != nil {
return callType
}
}
funcType := getResolvedType(call.Expression)
if funcType == nil {
return nil
}
nonNullishFunc := removeNullishFromType(funcType)
if nonNullishFunc == nil {
return nil
}
// If it's a union type of functions, check each part's return type
// e.g., (() => undefined) | (() => number) should check if any returns nullish
if utils.IsUnionType(nonNullishFunc) {
// For union of functions, check if any function returns a nullish type
// If so, the optional chaining result can be nullish
parts := nonNullishFunc.Types()
for _, part := range parts {
sigs := ctx.TypeChecker.GetCallSignatures(part)
if len(sigs) > 0 {
retType := ctx.TypeChecker.GetReturnTypeOfSignature(sigs[0])
if retType != nil && isNullishType(retType) {
// At least one function returns nullish, so use full expression type
// which includes all possible return types
return ctx.TypeChecker.GetTypeAtLocation(callExpr)
}
}
}
// No function returns nullish, get first signature's return type
signatures := ctx.TypeChecker.GetCallSignatures(nonNullishFunc)
if len(signatures) > 0 {
return ctx.TypeChecker.GetReturnTypeOfSignature(signatures[0])
}
return nil
}
signatures := ctx.TypeChecker.GetCallSignatures(nonNullishFunc)
if len(signatures) == 0 {
return nil
}
return ctx.TypeChecker.GetReturnTypeOfSignature(signatures[0])
💡 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.
Merge activity
|
0a93370 to
be8805a
Compare
d2e80b1 to
3e5b988
Compare
Merge activity
|

fixes #790