我正在尝试为此代码构建typescript定义文件(在myscript.ts
中):
var rectangle = new Rectangle(new Point(20, 20), new Size(60, 60));
var path = new Path.Rectangle(rectangle);
path.strokeColor = 'black';
请注意,第一个Rectangle与第二个(Path.Rectangle
)不同。
这就是我现在所拥有的(在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
}
}
使用此定义,以下两行都失败了:
var path = new Path.Rectangle(rectangle);
var upperLeft = path.bounds.topLeft;
我理解为什么但不知道如何修复定义。谢谢你的帮助。
答案 0 :(得分:2)
根据@xmojmr评论,我找到了一个有效的定义:
我的第一次尝试:
declare class Rectangle {
constructor(point: Point, size: Size);
topLeft: Point;
bottomRight: Point;
}
成为:
declare class NumericRectangle { // <=================== renamed
constructor(point: Point, size: Size);
topLeft: Point;
bottomRight: Point;
}
declare class Rectangle extends NumericRectangle { // <=================== added
}
...和
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
}
}
...变成:
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
}
}