【发布时间】:2015-01-30 20:58:51
【问题描述】:
我正在尝试为此代码构建打字稿定义文件(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;
我明白原因,但不知道如何修正定义。感谢您的帮助。
【问题讨论】:
-
如果我是你,我会更改名称,不确定是否有其他解决方案。
-
@Omri:我无法更改名称,因为代码来自外部库:paperjs。事实上原始代码来自这里:paperjs.org/reference/path/#path-rectangle-rectangle
-
可能没有简单而好的方法,但您应该能够通过声明一个假名称别名来解决它(例如
declare interface __r extends Rectangle {}并使用别名__r而不是全局范围@987654331 @,类似于Stack Overflow: TypeScript class with same name as interface中使用的方式 -
@xmojmr 根据您的评论,我找到了一个有效的定义,见下文。感谢您的帮助!
标签: javascript typescript