9
I’m having trouble overloading methods in Typescript.
The method to be overloaded is a Object Factory rect()
one signature has four numerical parameters and the other signature has only one parameter of the defined structure object type.
/**
* Esse é um dos métodos que quero sobrecarregar,
* com quatro parametros na assinatura.
*
* @param left
* @param top
* @param width
* @param height
*/
static rect(left: number, top: number, width: number, height: number): Rect2D {
return new Rect2D(left, top, width, height);
}
/**
* Esse é segundo método para sobrecarga,
* conta com apenas um parâmetro estruturado na assinatura.
*
* @param param0
*/
static rect({ left, top, width, height }: { left: number; top: number; width: number; height: number; }): Rect2D {
return new Rect2D(left, top, width, height);
}
How to do this overhead since the way I am doing results in the following error message:
Duplicate Function implementation.ts(2393)
I believe it is not relevant to the answer but if anyone asks here is the context fragment containing the methods to be overloaded:
class Point2D{
private _x : number;
public get x() : number {
return this._x;
}
public set x(v : number) {
this._x = v;
}
private _y : number;
public get y() : number {
return this._y;
}
public set y(v : number) {
this._y = v;
}
constructor(x: number, y: number){
this._x = x;
this._y = y;
}
}
class Rect2D implements IRect2D{
private _position: Point2D;
public get position(): Point2D {
return this._position;
}
public set position(value: Point2D) {
this._position = value;
}
private _size: Size;
public get size(): Size {
return this._size;
}
public set size(value: Size) {
this._size = value;
}
/**
* Esse é um dos métodos que quero sobrecarregar,
* com quatro parametros na assinatura.
*
* @param left
* @param top
* @param width
* @param height
*/
static rect(left: number, top: number, width: number, height: number): Rect2D {
return new Rect2D(left, top, width, height);
}
/**
* Esse é segundo método para sobrecarga,
* conta com apenas um parâmetro estruturado na assinatura.
*
* @param param0
*/
static rect({ left, top, width, height }: { left: number; top: number; width: number; height: number; }): Rect2D {
return new Rect2D(left, top, width, height);
}
private constructor(left: number, top: number, width: number, height: number ){
this._position.x = left;
this._position.y = top;
this._size.width = width;
this._size.height = height;
}
}
Does this help or duplicate? https://answall.com/q/365879/101 and https://answall.com/q/158075/101
– Maniero