Skip to content

Return value of super() calls not used for this #7574

Description

If a constructor returns a value other than this, then in subclasses super() calls to that constructor should use the result as this in the subclass constructor.

I believe this is specified in 12.3.5.1, step 10 of SuperCall: http://www.ecma-international.org/ecma-262/6.0/index.html#sec-super-keyword

Getting this behavior correct is going to be very important for supporting Custom Elements, which takes advantage of this to initialize browser-allocated elements with user-written constructors. See https://w3c.github.io/webcomponents/spec/custom/#htmlelement-constructor

Note, Babel 6 implements this correctly.

TypeScript Version:

1.8.9

Code

classFoo{x : number;constructor(){return{x: 1,};}}classBarextendsFoo{y : number;constructor(){super();this.y=2;}}leto=newBar();console.assert(o.x===1&&o.y===2);

Expected behavior:

No assertion error

The constructor for Bar should compile to this:

varBar=(function(_super){__extends(Bar,_super);functionBar(){var_this=_super.call(this);_this.x=1;return_this;}returnBar;}(Foo));

Actual behavior:

Assertion error

The constructor for Bar compiles to this:

varBar=(function(_super){__extends(Bar,_super);functionBar(){_super.call(this);this.x=1;}returnBar;}(Foo));

Metadata

Metadata

Labels

BugA bug in TypeScriptES6Relates to the ES6 SpecFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions