-
Notifications
You must be signed in to change notification settings - Fork 13k
Description
TypeScript Version: 3.7.0-beta
Search Terms: public class fields
Code:
class Base {}
class Sub extends Base {
// @ts-ignore
constructor() {
console.log('hi');
super();
}
field = 0;
}
new Sub();
Expected behavior:
The input code is valid (future) JS that does not throw. So the emitted code should equally not throw.
Actual behavior:
The emitted code throws ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor
.
The reason is that if a sub-class has field initializers, TypeScript currently requires the first line of the constructor to be super()
. This is a restriction of TypeScript, not of JavaScript. If you violate this rule, the emitted code crashes.
Emitted code:
class Base {}
class Sub extends Base {
// @ts-ignore
constructor() {
this.field = 0;
console.log('hi');
super();
}
}
new Sub();
Playground Link: Playground repro
Related Issues: Issue 33751 was marked as a duplicate and auto-closed. I think this missed my comment so opening this as an independent issue.