-
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
Class declaration not checked for compatibility with Object.prototype #15453
Comments
I agree it's unsound, but it'd also be strange/annoying to effectively say (by the same argument) that e.g. |
Hmm, yeah. I agree it's definitely sacrificing convenience for soundness. Not sure what the calculus is for making those tradeoffs for your team, so I'll trust the experts! |
I was vaguely surprised that this wasn't an error in Flow (it is in TS): const j = { toString: true };
var m: Object = j;
m.toString(); Does |
You're right to be surprised; It should be an error. I'm leaning toward making the literal itself an error (the strange/annoying solution). |
Just playing with it more... interesting behavior! It's hard to reckon what the right behavior is. const j = { toString: true };
var a = j; // OK
var b = { toString: j.toString }; // OK
var c: {} = j; // OK
c.toString(); // OK
var x = { ...j }; // Error (?!)
Not sure if Flow allows people to add things to the global type of all |
Yeah, we're inconsistent about what we check against Object.prototype (for now). If you take the stance that all object should be subtypes of the Object.prototype, things become concrete if inflexible. |
It's a bit weird though that class Foo extends Object {
toString() {
return 1;
}
} is an error and class Foo {
toString() {
return 1;
}
} is not, while semantically this is exactly the same thing. |
It is to some extent, but I think our view up until this point has been that |
Just thinking... if soundness is the goal, the only plausible fix is to disallow calls to things from var x = "to" + "String";
var j = { [x]: 20 };
j.toString(); // 😱 |
For sure, and I don't think anyone is suggesting that soundness is binary 100% or 0% (when it comes to static analysis of JS, that is). It's all about trade-offs, and I was just hoping to clarify what kind of trade-off was being made here, and hopefully to get a sense of what the calculus is for deciding those trade-offs. |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
TypeScript Version: Whatever is running on the TypeScript playground
Code
Expected behavior:
The declaration of C should be an error, because
toString
is not compatible with the implementation in Object.prototype.Actual behavior:
There is no error. Calling
f
with an instance ofC
results in a number at runtime where we expect a string.The text was updated successfully, but these errors were encountered: