-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Copilot
wants to merge
6
commits into
main
Choose a base branch
from
copilot/fix-46709
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
58449bb
Initial plan
Copilot 6355df5
Initial exploration and test case creation
Copilot c3df353
Implement fix for this type comparison false positive
Copilot 5754edc
Final fix verification and code formatting
Copilot ba8ae71
Move this check and remove unnecessary symmetry
RyanCavanaugh dd2d1c5
Was actually needed
RyanCavanaugh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
//// [tests/cases/compiler/thisTypeComparison.ts] //// | ||
|
||
//// [thisTypeComparison.ts] | ||
class AA { | ||
do1() { | ||
const b = dd.getB(); | ||
if (this === b) { | ||
console.log("this === b"); | ||
} | ||
} | ||
} | ||
|
||
class BB extends AA { | ||
getB(): BB { return this; } | ||
} | ||
|
||
let dd = new BB(); | ||
dd.do1(); | ||
|
||
//// [thisTypeComparison.js] | ||
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
if (typeof b !== "function" && b !== null) | ||
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
var AA = /** @class */ (function () { | ||
function AA() { | ||
} | ||
AA.prototype.do1 = function () { | ||
var b = dd.getB(); | ||
if (this === b) { | ||
console.log("this === b"); | ||
} | ||
}; | ||
return AA; | ||
}()); | ||
var BB = /** @class */ (function (_super) { | ||
__extends(BB, _super); | ||
function BB() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
BB.prototype.getB = function () { return this; }; | ||
return BB; | ||
}(AA)); | ||
var dd = new BB(); | ||
dd.do1(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//// [tests/cases/compiler/thisTypeComparison.ts] //// | ||
|
||
=== thisTypeComparison.ts === | ||
class AA { | ||
>AA : Symbol(AA, Decl(thisTypeComparison.ts, 0, 0)) | ||
|
||
do1() { | ||
>do1 : Symbol(AA.do1, Decl(thisTypeComparison.ts, 0, 10)) | ||
|
||
const b = dd.getB(); | ||
>b : Symbol(b, Decl(thisTypeComparison.ts, 2, 13)) | ||
>dd.getB : Symbol(BB.getB, Decl(thisTypeComparison.ts, 9, 21)) | ||
>dd : Symbol(dd, Decl(thisTypeComparison.ts, 13, 3)) | ||
>getB : Symbol(BB.getB, Decl(thisTypeComparison.ts, 9, 21)) | ||
|
||
if (this === b) { | ||
>this : Symbol(AA, Decl(thisTypeComparison.ts, 0, 0)) | ||
>b : Symbol(b, Decl(thisTypeComparison.ts, 2, 13)) | ||
|
||
console.log("this === b"); | ||
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) | ||
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
} | ||
} | ||
} | ||
|
||
class BB extends AA { | ||
>BB : Symbol(BB, Decl(thisTypeComparison.ts, 7, 1)) | ||
>AA : Symbol(AA, Decl(thisTypeComparison.ts, 0, 0)) | ||
|
||
getB(): BB { return this; } | ||
>getB : Symbol(BB.getB, Decl(thisTypeComparison.ts, 9, 21)) | ||
>BB : Symbol(BB, Decl(thisTypeComparison.ts, 7, 1)) | ||
>this : Symbol(BB, Decl(thisTypeComparison.ts, 7, 1)) | ||
} | ||
|
||
let dd = new BB(); | ||
>dd : Symbol(dd, Decl(thisTypeComparison.ts, 13, 3)) | ||
>BB : Symbol(BB, Decl(thisTypeComparison.ts, 7, 1)) | ||
|
||
dd.do1(); | ||
>dd.do1 : Symbol(AA.do1, Decl(thisTypeComparison.ts, 0, 10)) | ||
>dd : Symbol(dd, Decl(thisTypeComparison.ts, 13, 3)) | ||
>do1 : Symbol(AA.do1, Decl(thisTypeComparison.ts, 0, 10)) | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
//// [tests/cases/compiler/thisTypeComparison.ts] //// | ||
|
||
=== thisTypeComparison.ts === | ||
class AA { | ||
>AA : AA | ||
> : ^^ | ||
|
||
do1() { | ||
>do1 : () => void | ||
> : ^^^^^^^^^^ | ||
|
||
const b = dd.getB(); | ||
>b : BB | ||
> : ^^ | ||
>dd.getB() : BB | ||
> : ^^ | ||
>dd.getB : () => BB | ||
> : ^^^^^^ | ||
>dd : BB | ||
> : ^^ | ||
>getB : () => BB | ||
> : ^^^^^^ | ||
|
||
if (this === b) { | ||
>this === b : boolean | ||
> : ^^^^^^^ | ||
>this : this | ||
> : ^^^^ | ||
>b : BB | ||
> : ^^ | ||
|
||
console.log("this === b"); | ||
>console.log("this === b") : void | ||
> : ^^^^ | ||
>console.log : (...data: any[]) => void | ||
> : ^^^^ ^^ ^^^^^ | ||
>console : Console | ||
> : ^^^^^^^ | ||
>log : (...data: any[]) => void | ||
> : ^^^^ ^^ ^^^^^ | ||
>"this === b" : "this === b" | ||
> : ^^^^^^^^^^^^ | ||
} | ||
} | ||
} | ||
|
||
class BB extends AA { | ||
>BB : BB | ||
> : ^^ | ||
>AA : AA | ||
> : ^^ | ||
|
||
getB(): BB { return this; } | ||
>getB : () => BB | ||
> : ^^^^^^ | ||
>this : this | ||
> : ^^^^ | ||
} | ||
|
||
let dd = new BB(); | ||
>dd : BB | ||
> : ^^ | ||
>new BB() : BB | ||
> : ^^ | ||
>BB : typeof BB | ||
> : ^^^^^^^^^ | ||
|
||
dd.do1(); | ||
>dd.do1() : void | ||
> : ^^^^ | ||
>dd.do1 : () => void | ||
> : ^^^^^^^^^^ | ||
>dd : BB | ||
> : ^^ | ||
>do1 : () => void | ||
> : ^^^^^^^^^^ | ||
|
66 changes: 66 additions & 0 deletions
66
tests/baselines/reference/thisTypeComparisonExtended.errors.txt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
thisTypeComparisonExtended.ts(26,13): error TS2367: This comparison appears to be unintentional because the types 'this' and 'CC' have no overlap. | ||
|
||
|
||
==== thisTypeComparisonExtended.ts (1 errors) ==== | ||
// Test 1: Original issue - this === subclass instance should work | ||
class AA { | ||
do1() { | ||
const b = dd.getB(); | ||
if (this === b) { // Should not error | ||
console.log("this === b"); | ||
} | ||
} | ||
} | ||
|
||
class BB extends AA { | ||
getB(): BB { return this; } | ||
} | ||
|
||
let dd = new BB(); | ||
dd.do1(); | ||
|
||
// Test 2: this === unrelated class should still error | ||
class CC { | ||
value: number = 42; | ||
} | ||
|
||
class DD { | ||
test() { | ||
const c = new CC(); | ||
if (this === c) { // Should still error - no relationship | ||
~~~~~~~~~~ | ||
!!! error TS2367: This comparison appears to be unintentional because the types 'this' and 'CC' have no overlap. | ||
console.log("unrelated"); | ||
} | ||
} | ||
} | ||
|
||
// Test 3: Multiple inheritance levels | ||
class EE extends BB { | ||
getE(): EE { return this; } | ||
} | ||
|
||
class FF extends EE { | ||
testMultiLevel() { | ||
const e = new EE(); | ||
if (this === e) { // Should not error - FF extends EE | ||
console.log("multi-level inheritance"); | ||
} | ||
} | ||
} | ||
|
||
// Test 4: Interface implementation | ||
interface ITest { | ||
getValue(): number; | ||
} | ||
|
||
class GG implements ITest { | ||
getValue() { return 42; } | ||
|
||
testInterface() { | ||
const impl: ITest = new GG(); | ||
if (this === impl) { // Should not error | ||
console.log("interface implementation"); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.