Skip to content

Constructor property inheritance is not giving an error in ES2015+ target #29328

@alfaproject

Description

@alfaproject

TypeScript Version: 3.2.2

Search Terms: property inheritance constructor super

Code

abstract class A {
  public names = ['A'];
}

class B extends A {
  public names = [...super.names, 'B'];
}

const b = new B();
alert(b.names);

Expected behavior:
ES2015+ target: either TypeScript downlevels super.names in the child class to this.names which would make that code work, or it should give an error like it does in the ES5 version (check playground link).

Working ES2015 version:

class A {
  constructor() {
    this.names = ['A'];
  }
}

class B extends A {
  constructor() {
    super(...arguments);
    this.names = [...this.names, 'B']; // `super` -> `this` so that this works as expected, but maybe not correct, not sure of the implications.
  }
}

const b = new B();
alert(b.names);

So either that, or give a compile error. (ES5 target gives a compile error that has nothing to do with the actual error, so maybe even that should be sorted out)

Actual behavior:
Compiles fine under ES2015+ target, but gives a runtime error. (check stackblitz link)

Playground Link: https://www.typescriptlang.org/play/index.html#src=abstract%20class%20A%20%7B%0A%20%20public%20names%20%3D%20%5B'A'%5D%3B%0A%7D%0A%0Aclass%20B%20extends%20A%20%7B%0A%20%20public%20names%20%3D%20%5B...super.names%2C%20'B'%5D%3B%0A%7D%0A%0Aconst%20b%20%3D%20new%20B()%3B%0Aalert(b.names)%3B%0A

Stackblitz Link: https://stackblitz.com/edit/typescript-jcg479

Related Issues: I've tried, but couldn't really find anything similar. Forgive me if it has been reported already.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions