-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
'this' type cannot be compared with subclasses #15748
Comments
if you intend to use class Superclass {
test(b:this):boolean {
let x;
x = this == b; // OK
return x;
}
} |
@mhegazy But I don't even use class Superclass {
test(b:Subclass):boolean {
return this == b || b.subfield>0; // TS2365
}
} |
class Superclass {
isMeOrPositive(b:Subclass):boolean {
return this == b || b.subfield>0; // TS2365
}
}
class Subclass extends Superclass {
constructor(public subfield:number){
super();
}
}
class Grandchild extends Subclass {
constructor(subfield:number,public name:String){
super(subfield);
}
}
let a = new Superclass();
let b = new Subclass(0);
let c = new Grandchild(10,"c'");
for (let i of [a,b,c]) console.log(i.isMeOrPositive(b),i.isMeOrPositive(c));
// false true
// true true
// false true If that compiler error is intended behaviour, what kind of situation it tries to prevent? |
@mhegazy I can't figure out why this behavior is desirable, though I do understand why it happens. What's conceptually different between this and the code below? var x: string | number = <any>null;
var y: number | boolean = <any>null;
x === y; |
This doesn't seem frequently-hit, and the rules for comparability are complex enough already that I don't think it's worthwhile to complicate them further to account for |
TypeScript Version: 2.3.2
Code
Produces error
TS2365:Operator '==' cannot be applied to types 'this' and 'Subclass'.
Same for
!=
,===
,!==
.Possibly related to #15615 .
The text was updated successfully, but these errors were encountered: