Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix false positive "no overlap" error when comparingthis 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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
Copilot wants to merge6 commits intomain
base:main
Choose a base branch
Loading
fromcopilot/fix-46709

Conversation

Copilot
Copy link
Contributor

@CopilotCopilotAI commentedJul 15, 2025
edited
Loading

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

Consider this example:

classAA{do1(){constb=dd.getB();if(this===b){// ❌ Error: no overlap between 'this' and 'BB'console.log("this === b");}}}classBBextendsAA{getB():BB{returnthis;}}letdd=newBB();dd.do1();// ✅ Correctly outputs "this === b" at runtime

Root Cause

TheisSimpleTypeRelatedTo function inchecker.ts did not handlethis type parameters specially for thecomparableRelation. When comparingthis (a type parameter) with a concrete class type, it failed to recognize that thethis type is constrained to a base class that could be related to the target type.

Solution

Added special handling inisSimpleTypeRelatedTo forthis type parameters when usingcomparableRelation. The fix "reverts"this types back to their constraint (the class type) for comparison purposes by:

  1. Detecting when source or target is athis type parameter (isThisType flag)
  2. Getting the constraint of thethis type parameter (the class it's bound to)
  3. UsingisTypeRelatedTo 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! Clickhere to start the survey.

@typescript-bottypescript-bot added the For Milestone BugPRs that fix a bug with a specific milestone labelJul 15, 2025
CopilotAIand others added3 commitsJuly 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>
@CopilotCopilotAI changed the title[WIP] False positive 'This condition will always return false' comparingthis with an instance of a subclassFix false positive "no overlap" error when comparingthis with subclass instancesJul 15, 2025
@CopilotCopilotAI requested a review fromRyanCavanaughJuly 15, 2025 04:06
Copilot finished work on behalf ofRyanCavanaughJuly 15, 2025 04:06
@RyanCavanaughRyanCavanaugh marked this pull request as ready for reviewJuly 15, 2025 17:43
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@RyanCavanaughRyanCavanaughRyanCavanaugh left review comments

At least 1 approving review is required to merge this pull request.

Labels
For Milestone BugPRs that fix a bug with a specific milestone
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

False positive 'This condition will always return false' comparingthis with an instance of a subclass
3 participants
@Copilot@RyanCavanaugh@typescript-bot

[8]ページ先頭

©2009-2025 Movatter.jp