javascript - TypeScript-definition: samma namn men olika typer inuti och utanför modulen?
Jag försöker skapa TypeScript-definitionfilen för denna kod (i myscript.ts) :
var rectangle = new Rectangle(new Point(20, 20), new Size(60, 60));
var path = new Path.Rectangle(rectangle);
path.strokeColor = 'black';
Observera att här är den första rektangeln en annan typ än den andra (Path.Rectangle).
Detta är vad jag har för nu (i 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
}
}
Med denna definition misslyckades båda följande rader:
var path = new Path.Rectangle(rectangle);
var upperLeft = path.bounds.topLeft;
Jag förstår varför men vet inte hur man åtgärdar definitionen. Tack för din hjälp.
Svar #1
Baserat på @xmojmr:s kommentar, hittade jag en giltig definition :
Mitt första försök:
declare class Rectangle {
constructor(point: Point, size: Size);
topLeft: Point;
bottomRight: Point;
}
blir :
declare class NumericRectangle { // <=================== renamed
constructor(point: Point, size: Size);
topLeft: Point;
bottomRight: Point;
}
declare class Rectangle extends NumericRectangle { // <=================== added
}
... och
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
}
}
... blir :
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
}
}
