Q:

Using Derived-Class Instance Inside Base-Class to Call Base-Class Method with Derived-class Data

i have a base generic component and i want to call derived method from base class that return data that belongs to derived class.
first problem is, i cant define it static in abstract class!
second problem is, when i create new instance of derived class with {} as TDomain inside base generic class, its empty and method return null ref error !
Base Entity is like this:
export abstract class BaseEntity
{
   abstract getColumns():string[];
}

My Model is Like this:
export class Person extends BaseEntity 
{
    getColumns() {
    return ["Id", "Name"];
  }
}

I have a generic base component:
@Component({
  selector: 'base-component',
  templateUrl: './base.component.html',
  styleUrls: ['./base.component.css']
})
export class BaseComponent<TDomain  extends BaseEntity,TService extends DataService > implements OnInit {
data:any[];

  constructor() {
  }

  ExportToExcel(): void {
    //must create an instance of derived class
    //then call getcolumns method of it
     let obj = {} as TDomain; // but its empty  doesnt work
    ExcelService.export(obj.getcolumns(this.data));

  }
    }

and derived component:
@Component({
  selector: 'Derived-Cmp',
  templateUrl: './derived.component.html',
  styleUrls: ['./derived.component.css']
})
export class DerivedComponent extends  BaseCrudComponent<Person,PersonService> implements OnInit {

  //ExportToExcel Method called form html 

}

A:

To create an instance of TDomain you need to pass the constructor of TDomain to the base class:
export abstract class BaseEntity {
    abstract getColumns(): string[];
}

export class Person extends BaseEntity {
    getColumns() { return ["Id", "Name"]; }
}
export class BaseComponent<TDomain extends BaseEntity> {
    data: any[];

    constructor(public ctor: new () => TDomain) {
    }

    ExportToExcel(): void {
        let obj = new this.ctor(); // create instance using the constructor
        obj.getColumns(); /// call method
    }
}

export class DerivedComponent extends BaseComponent<Person> {
    constructor() {
        super(Person)
    }
}

link form github repository call derived class method inside base generic class

