BaseObject: Generic Class with the properties of the generic type

Viewed 32

In Typescript I would like to create a BaseObject class that contains predefined methods for all objects that extend it. I would like this class to be built by extending the properties of a Model. The only way I have found to do this is to create a variable public _model: Model inside the class BaseObject but it is difficult to access the properties because I always have to use the variable _model.

export interface Interface{
  _prop1: string;
  _prop2: number;
  [...]
}

export class BaseObject<Model>{
  private _id: string;
  private _parentsID: string[] = [];
  public _model: Model;

  constructor(model: Model){
    super();
      this._id = uuid.v4();
      this._model = model;
  }
  [...]

export class Class extends BaseObject<Interface>{
  constructor(model: Interface){
    super(model);
      [...]
  }
  [...]
}

Is there a way to reach the following structure? The purpose is to maintain a basic data model for the API, to make the objects unique through an Id and to be able to assign them to a parent, and finally to create an editable class to use the object within Angular

export interface Interface{
  _prop1: string;
  _prop2: number;
  [...]
}

export class BaseObject<Model> extends <Model>{
  private _id: string;
  private _parentsID: string[] = [];

  constructor(){
    super();
      this._id = uuid.v4();
  }
  

  [...]

export class Class extends BaseObject<Interface>{
  constructor(){
    super();
      [...]
  }
  get propr1(){
    return this._prop1;
  }
  set propr2(value: number){
    this._propr2 = value;
  }
  [...]
}
0 Answers
Related