Skip to content
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

Closed
aimozg opened this issue May 10, 2017 · 5 comments
Closed

'this' type cannot be compared with subclasses #15748

aimozg opened this issue May 10, 2017 · 5 comments
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript

Comments

@aimozg
Copy link

aimozg commented May 10, 2017

TypeScript Version: 2.3.2

Code

class Superclass {
	test(b:Subclass):boolean {
		let x;
		x = this == b; // TS2365
		x = this as Superclass == b; // workaround
		x = this instanceof Subclass && this == b;
		return x;
	}
}
class Subclass extends Superclass {
	public subfield=0;
}

Produces error
TS2365:Operator '==' cannot be applied to types 'this' and 'Subclass'.
Same for !=, ===, !==.

Possibly related to #15615 .

@mhegazy
Copy link
Contributor

mhegazy commented May 10, 2017

this is the type of the most derived class at the time of instantiation, that can be something else than SubClass, consider class SubClass3 extends SubClass .. .

if you intend to use this, then type it as such:

class Superclass {
	test(b:this):boolean {
		let x;
		x = this == b; // OK
		return x;
	}
}

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label May 10, 2017
@aimozg
Copy link
Author

aimozg commented May 10, 2017

@mhegazy But I don't even use this as a type. I just have a method in a superclass that is aware of subclass existance and want to perform a strict reference equality check. And the method parameter must be Subclass.

class Superclass {
	test(b:Subclass):boolean {
		return this == b || b.subfield>0; // TS2365
	}
}

@aimozg
Copy link
Author

aimozg commented May 10, 2017

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?

@RyanCavanaugh
Copy link
Member

@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;

@mhegazy mhegazy added Bug A bug in TypeScript and removed Question An issue which isn't directly actionable in code labels May 11, 2017
@mhegazy mhegazy added Suggestion An idea for TypeScript In Discussion Not yet reached consensus and removed Bug A bug in TypeScript labels Jul 21, 2018
@RyanCavanaugh RyanCavanaugh added Declined The issue was declined as something which matches the TypeScript vision and removed In Discussion Not yet reached consensus labels Dec 16, 2021
@RyanCavanaugh
Copy link
Member

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 this. The semantics here are consistent with other places where we disallow ===, even though it's known to generate false positives.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants