-
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
ES5: inheritance of static properties is incorrect #4266
Comments
I think the difference has to do with our downlevel emit not relying on either the ES5.1 Conveniently, you can define your own global |
Yes, this is basically the same as #1601. I kinda feel bad about it because with the "modern" web stack it can easily screw you when you start using advanced features, such as metadata. I stumbled upon it by accident and it took me some time to figure it all out. I am relying on Babel for the ES5 generation and your comment makes me realize that it won't work in IE < 11. In fact Babel is a no-op if there is no support for Babel at least works for modern browsers (IE11), TS doesn't. Neither work exactly as it should in older browser, it's arguable which behavior is best... I understand the argument in #1601 that you don't want to take a dependency "modern" features for the codegen, although I don't agree. Given that other codegen is correct in some browsers rather than always wrong; and given that people can choose to opt in less correct codegen by providing their own Anyway, whatever the decision is I think this issue needs more visibility. Maybe you should provide alternate |
I think the general observation is that it's better to be slightly incorrect, but consistent, than to be correct on some platforms and incorrect on others. Browser compat testing is already a nightmare and we don't want to make it worse by having silently different behavior on different browsers. The fact that you became aware of this now, rather than way later when you had your code running on IE10, is a very good thing. |
I see your point, one way or another you can't be correct in all situations. Being consistent on all platform is a good thing, indeed. I still think that the metadata apis are going to make this issue very visible, yet I am unsure what you should do about it. Maybe limit attributes usage to ES6+ codegen? Along with some documentation of why this is forbidden (or behind a flag) that would make people aware of the pitfall? |
Assume
The default
__extends
is copying each own property ofB
toC
. This is a problem because it means that a property ofB
becomes indistinguishable from a property ofC
(by virtue of copy, it is its own).This doesn't seem like much, but sometimes you need each class to have a different value for a property.
For a real life use-case, I was bitten here: aurelia/metadata#22.
Other use-cases that occured to me because of this bug:
WeakMap
polyfills stores data in the object under a random key. Because of the codegen above,WeakMap
doesn't work properly forB
andC
.By virtue of that,
Reflect.metadata
polyfill is not working in IE9 and 10 (it relies onWeakMap
).Contrast with Babel, who just chains
prototype
. This way, one can checkhasOwnProperty()
.An artificial example showing the different behavior between TS and Babel:
http://www.typescriptlang.org/Playground#src=class%20B%20%7B%7D%0AB%5B%22__metadata__%22%5D%20%3D%204%3B%0Aclass%20C%20extends%20B%20%7B%7D%0Aif%20(C.hasOwnProperty(%22__metadata__%22))%0A%09alert(C%5B%22__metadata__%22%5D)%3B
http://babeljs.io/repl/#?experimental=true&evaluate=true&loose=false&spec=false&code=class%20B%20%7B%7D%0AB.__metadata__%20%3D%204%3B%0Aclass%20C%20extends%20B%20%7B%7D%0Aif%20(C.hasOwnProperty(%22__metadata__%22))%0A%20%20alert(C.__metadata__)%3B
The text was updated successfully, but these errors were encountered: