Skip to content

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

Closed
Copilot wants to merge 6 commits intomainfrom
copilot/fix-46709
Closed

Fix false positive "no overlap" error when comparing this with subclass instances#62070
Copilot wants to merge 6 commits intomainfrom
copilot/fix-46709

Conversation

Copy link
Copy Markdown
Contributor

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 AI and others added 3 commits July 15, 2025 03:53
Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
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 AI requested a review from RyanCavanaugh July 15, 2025 04:06
@RyanCavanaugh RyanCavanaugh marked this pull request as ready for review July 15, 2025 17:43
@typescript-bot
Copy link
Copy Markdown
Collaborator

With 6.0 out as the final release vehicle for this codebase, we're closing all PRs that don't fit the merge criteria for post-6.0 patches. If you think this was a mistake and this PR fits the post-6.0 patch criteria, please post to the 6.0 iteration issue with details (specifically, which PR and which patch criteria it satisfies).

Next steps for PRs:

  • For crash bugfixes or language service improvements, PRs are currently accepted at the typescript-go repo
  • Changes to type system behavior should wait until after 7.0, at which point mainline TypeScript development will resume in this repository with the Go codebase
  • Library file updates (lib.d.ts etc) continue to live in this repo or the DOM Generator repo as appropriate

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