TypeScript: 'super' must be called before before accessing 'this' in the constructor of a derived class TypeScript: 'super' must be called before before accessing 'this' in the constructor of a derived class typescript typescript

TypeScript: 'super' must be called before before accessing 'this' in the constructor of a derived class


Another solution I eventually came up with, in addition to the ones provided by @iberbeu and @Nypan, is to add and intermediary initProps() method right before the call to scream():

class Person {    public firstName: string;    constructor(firstName: string, props?: any)    {        this.firstName = firstName;        this.initProps(props);        this.scream();    }    protected initProps(props: any): void    {    }    protected scream(): void    {        console.log(this.firstName);    }}class Employee extends Person{    public lastName: string;    constructor(firstName: string, lastName: string)    {        super(firstName, {lastName});    }    protected initProps(props: any): void    {        this.lastName = props.lastName;    }    protected scream(): void    {        console.log(this.firstName + ' ' + this.lastName);    }}

Although I think both made a strong point and I should actually be using a factory pattern instead..


Am I doing a dumb thing here?

Yes you are. As iberbeu said in his comment a constructor should never do anything that does not have to do with constructing the object. It is a case of bad practice that can lead to all sorts of unexpected behaviour.

Are there better ways to arrange my code so I don't need to do this?

Using the solution you provided in your C option is the way to go here.

Is there some way to work around this error?

It depends on what you actually want to do. The normal way of doing things is illustrated by yourself in your C option. If the problem you are having is related to actually instantiating complex objects you might want to look in to builder/factory patterns. But if you actually want the constructors to do something you are simply doing it wrong; constructors are not ment to perform actions, they are there to construct objects and nothing else.


As I wrote in my comment and @Nypan in his answer, you should avoid doing this. Anyway, a possibility could be to override the method scream in the Child and call a new method. Take a look at the following code

class Person {    public firstName: string;    constructor() {        this.scream();    }    protected scream(): void {        console.log(this.firstName);    }}class Employee extends Person{    public lastName: string;    constructor(firstName: string, lastName: string)    {        super(firstName);        this.lastName = lastName;        this.screamOVerriden();    }    protected scream(): void {        // do nothing    }    protected screamOverriden(): void {        console.log(this.firstName + ' ' + this.lastName);    }}

I still don't recommend doing this but if you say you really need it and you don't care about doing it properly then this could be one solution