-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Using private or protected make type checking nominal instead of structural #7755
Comments
Consider something like this class Foo {
private versionNumber = 10;
public compareTo(other: Foo) {
return this.getVersion() === other.getVersion();
}
private getVersion() {
return this.versionNumber;
}
}
class Bar {
private myName = 'default;'
public compareTo(other: Bar) {
return this.myName === other.myName;
}
private getVersion() {
/* ... DANGEROUS CODE HERE ...*/
return -1;
}
} By inspection, But this code does invoke let f = new Foo();
let b = new Bar();
f.compareTo(b); |
@RyanCavanaugh I can't see the issue. It sounds like you're saying to use Edit: For example, switch to public and the same situation can occur, why is class Foo {
public versionNumber = 10;
public compareTo(other: Foo) {
return this.getVersion() === other.getVersion();
}
public getVersion() {
return this.versionNumber;
}
}
class Bar {
public versionNumber = -1;
public myName = 'default;'
public compareTo(other: Bar) {
return this.myName === other.myName;
}
public getVersion() {
/* ... DANGEROUS CODE HERE ...*/
return -1;
}
}
let f = new Foo();
let b = new Bar();
f.compareTo(b); |
I don't understand what's a hack about the idea of method visibility. A This should include the case where the person who isn't you happens to be structurally compatible with you, no? |
The hack isn't method visibility, that's fine. The part that sounds hacky is using
Yes, absolutely. I overlooked Edit: is -> isn't. |
Allowing a class to invoke its own private members on another instance of that same class was an intentional design decision. This a very common thing in OOP languages and we took precedent from that. It'd be pretty cumbersome if we disallowed it (I suspect most people would just cast to |
Thanks, I realised working thinking about your example. It still feels like a bit of a wart on the type system that will be surprising to people. Proper detection of the use-case would require reverse flow analysis, I suppose? Not sure what you'd call it, you could check the compatibility of |
Ok, I guess the solution here is to make a common interface. Thanks. Edit: I'm still not convinced though 😄 There's plenty of times when two different instances may be created, even accidentally such as in the linked use-cases or across execution frames. It feels odd to have an artificial restriction here when JavaScript itself has a dozen ways you can end up with separate instances at runtime too (where the structural types say 👍, but |
From typings/typings#380. It's definitely unexpected and I can't understand why, but I see in the specification it's intentional. For example:
Expected behavior:
No error, they are structurally the same. Private and protected methods can not be used outside of the class, so it shouldn't affect assignability.
Actual behavior:
Edit: From the issue in
typings
, I also linked to #6496 and #6365 which appear to describe the issue but only dive into workarounds instead of validating why this is happening.The text was updated successfully, but these errors were encountered: