Skip to content

Fix false positive "no overlap" error when comparing this with subclass instances #62070

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 15, 2025

TypeScript incorrectly reported error TS2367 "This comparison appears to be unintentional because the types 'this' and 'BB' have no overlap" when comparing this with an instance of a subclass, even though the comparison is legitimate and works correctly at runtime.

Consider this example:

class AA {
    do1() {
        const b = dd.getB();
        if (this === b) {  // ❌ Error: no overlap between 'this' and 'BB'
            console.log("this === b");
        }
    }
}

class BB extends AA {
    getB(): BB { return this; }
}

let dd = new BB();
dd.do1(); // ✅ Correctly outputs "this === b" at runtime

Root Cause

The isSimpleTypeRelatedTo function in checker.ts did not handle this type parameters specially for the comparableRelation. When comparing this (a type parameter) with a concrete class type, it failed to recognize that the this type is constrained to a base class that could be related to the target type.

Solution

Added special handling in isSimpleTypeRelatedTo for this type parameters when using comparableRelation. The fix "reverts" this types back to their constraint (the class type) for comparison purposes by:

  1. Detecting when source or target is a this type parameter (isThisType flag)
  2. Getting the constraint of the this type parameter (the class it's bound to)
  3. Using isTypeRelatedTo to check if the constraint is comparable to the other type

Testing

  • Original issue fixed: this === subclassInstance no longer errors
  • Legitimate errors preserved: this === unrelatedClass still correctly errors
  • Multi-level inheritance works: this === inheritedInstance allowed
  • Interface comparisons work: this === interfaceImplementation allowed
  • No regressions: All related type comparison tests continue passing

The fix is minimal and surgical - only 11 lines of code added to handle this specific case while preserving all existing behavior.

Fixes #46709.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@typescript-bot typescript-bot added the For Milestone Bug PRs that fix a bug with a specific milestone label Jul 15, 2025
@Copilot Copilot AI changed the title [WIP] False positive 'This condition will always return false' comparing this with an instance of a subclass Fix false positive "no overlap" error when comparing this with subclass instances Jul 15, 2025
@Copilot Copilot AI requested a review from RyanCavanaugh July 15, 2025 04:06
Copilot finished work on behalf of RyanCavanaugh July 15, 2025 04:06
@RyanCavanaugh RyanCavanaugh marked this pull request as ready for review July 15, 2025 17:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Milestone Bug PRs that fix a bug with a specific milestone
Projects
None yet
Development

Successfully merging this pull request may close these issues.

False positive 'This condition will always return false' comparing this with an instance of a subclass
3 participants