google / google/closure-compiler
Weird behavior redefining constructors
- Dominant language
- JavaScript
- Stars
- 7.7k
- Forks
- 1.2k
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 6
Description
This doesn't cause any type warnings. The type system merges the properties of the first and second declaration of `C` into a single type.
``` javascript
var C = class {
f() {}
};
C = class {
g() {}
};
var n = new C();
n.f();
n.g();
```
There are certain situations where we do get errors for redefining classes. `FunctionTypeBuilder` throws an error when the constructor signatures are different.
If the second class has a supertype, the supertype doesn't get put into the actual FunctionType, but it does result in a $jscomp.inherits() call that causes an error if it's incompatible with the first definition's superclass.
It's difficult to do this correctly for ES5-style classes because we don't have a single point that declares the class/properties, but prototype properties potentially scattered throughout the code. This could be easier for ES6-style classes because there is a single CLASS node representing the class declaration. We could check for structural compatibility there.
Contributor guide
Assessment
This issue has not been assessed yet.