Búsqueda de sitios web

javascript - Definición de TypeScript: ¿mismo nombre pero diferentes tipos dentro y fuera del módulo?


Estoy intentando crear el archivo de definición de TypeScript para este código (en myscript.ts) :

var rectangle = new Rectangle(new Point(20, 20), new Size(60, 60));
var path = new Path.Rectangle(rectangle);
path.strokeColor = 'black';

Tenga en cuenta que aquí el primer Rectángulo es de un tipo diferente al segundo (Path.Rectangle).

Esto es lo que tengo por ahora (en myscript.d.ts) :

declare class Point {
    constructor(x: number, y: number);
    add: (something: number[]) => Point;
}
declare class Size {
    constructor(width: number, height: number);
}
declare class Rectangle {
    constructor(point: Point, size: Size);
    topLeft: Point;
    bottomRight: Point;
}
declare module Path {
    class PathBase {
        strokeColor: string;
        bounds: Rectangle; // <== here I need the Rectangle type defined outside of the Path module
        fillColor: Color;
    }

    export class Rectangle extends PathBase {
        constructor(point: Point, size: Size);
        constructor(rec : Rectangle); // <== here I need the Rectangle type defined outside of the Path module
    }
}

Con esta definición, ambas líneas siguientes fallaron :

var path = new Path.Rectangle(rectangle);
var upperLeft = path.bounds.topLeft;

Entiendo por qué pero no sé cómo arreglar la definición. Gracias por tu ayuda.

Respuesta #1

Basado en el comentario de @xmojmr, encontré una definición válida :

Mi primer intento :

declare class Rectangle {
    constructor(point: Point, size: Size);
    topLeft: Point;
    bottomRight: Point;
}

se convierte en :

declare class NumericRectangle { // <=================== renamed
    constructor(point: Point, size: Size);
    topLeft: Point;
    bottomRight: Point;
}

declare class Rectangle extends NumericRectangle {  // <=================== added
}

... y

declare module Path {
    class PathBase {
        strokeColor: string;
        bounds: Rectangle; // <== here I need the Rectangle type defined outside of the Path module
        fillColor: Color;
    }

    export class Rectangle extends PathBase {
        constructor(point: Point, size: Size);
        constructor(rec : Rectangle); // <== here I need the Rectangle type defined outside of the Path module
    }
}

... se convierte en :

declare module Path {
    class PathBase {
        strokeColor: string;
        bounds: NumericRectangle;  // <=================== modified
        fillColor: Color;
    }

    export class Rectangle extends PathBase {
        constructor(point: Point, size: Size);
        constructor(rec: NumericRectangle); // <=================== modified
    }
}