I am trying to make a class in TypeScript, in the following way:
You can receive a Array<Array<string>>
only or you can receive several parameters ( ancho : number
, blanco : string
, trozo : string
)
The implementation suggests me to use sobrecarga de constructores
, but this is a problem since in TypeScript the created constructors must be compatible , so we must implement the constructors with all the class variables but not they are explicitly needed in that constructor, although they can be declared optional (In fact, a constructor is implemented that is compatible with all the others, the other constructors are simply declared):
class Piramide {
public array : Array<Array<String>> | Array<any>;
public ancho : number;
public blanco : string;
public trozo : string;
constructor(array : Array<Array<String>> | Array<any>, ancho? : number, blanco? : string, trozo? : string);
constructor(array : Array<Array<String>> | Array<any>, ancho : number, blanco : string, trozo : string){
if(!array){
this.ancho = ancho;
this.blanco = blanco;
this.trozo = trozo;
this.array = [];
}
else
this.array = array;
this.ancho = array.length;
...
}
...
}
Is there any way to make this more understandable and readable?