Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call constructor from derived type via this in typescript

In my typescript I'm trying to create/clone an child-object via a method in the base-class. This is my (simplified) setup.

abstract class BaseClass<TCompositionProps> {
    protected props: TCompositionProps;

    protected cloneProps(): TCompositionProps { return $.extend(true, {}, this.props); } // can be overwriten by childs

    constructor(props: TCompositionProps){
        this.props = props;
    }

    clone(){
        const props = this.cloneProps();
        return this.constructor(props);
    }   
}

interface IProps {
    someValues: string[];
}

class Child extends BaseClass<IProps>{
    constructor(props: IProps){
        super(props);
    }
}

Now, I'm going to create a new object

const o1 = new Child({someValues: ["This","is","a","test"]};

// get the clone
const clone = o1.clone();

The constructor is hit (but it's just the call to the function), meaning there is no new object created. When using return Child.prototype.constructor(props) instead I get my new object.

So how can I call the constructor of Child in it's base-class?

Also tried this

like image 939
KingKerosin Avatar asked Aug 24 '17 20:08

KingKerosin


People also ask

How do you call a constructor from a class in TypeScript?

You can call the base class constructor from the child class by using the super() which will execute the constructor of the base class. Example: Javascript.

How do you use constructors in TypeScript?

The TypeScript docs have a great example of constructor usage: class Greeter { greeting: string; constructor(message: string) { this. greeting = message; } greet() { return "Hello, " + this. greeting; } } let greeter = new Greeter("world");

How do you call a super method in TypeScript?

The super keyword can be used in expressions to reference base class properties and the base class constructor. Super calls consist of the keyword super followed by an argument list enclosed in parentheses. Super calls are only permitted in constructors of derived classes.

How do you use this in TypeScript?

The "this" keyword always points to the object that is calling a particular method. The type of "this" in an expression depends on the location in which the reference occurs: In a constructor, member function, or member accessor, this is of the class instance type of the containing class.


1 Answers

You can invoke the constructor with the new operator, that seems to work. Also I would use this for the return type so that the clone method will return the derived type not the base type

abstract class BaseClass<TCompositionProps> {
    protected props: TCompositionProps;

    protected cloneProps(): TCompositionProps { return $.extend(true, {}, this.props); } 

    constructor(props: TCompositionProps){
        this.props = props;
    }

    clone() : this{
        const props = this.cloneProps();
        return new (<any>this.constructor)(props);
    }   
}
like image 185
Titian Cernicova-Dragomir Avatar answered Sep 28 '22 09:09

Titian Cernicova-Dragomir