3
我试图建立该代码打字稿定义文件(myscript.ts
):TypeScript定义:模块内部和外部的名称相同但名称不同?
var rectangle = new Rectangle(new Point(20, 20), new Size(60, 60));
var path = new Path.Rectangle(rectangle);
path.strokeColor = 'black';
请注意,这里第一个矩形是从第二(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;
我明白为什么,但不知道如何修复定义。谢谢你的帮助。
如果我是你,我会更改名称,不确定是否有其他解决方案。 – 2014-12-02 15:33:47
@Omri:我无法更改名称,因为代码来自外部lib:paperjs。实际上,原始代码来自这里:http://paper.jp/reference/path/#path-rectangle-rectangle – JYL 2014-12-02 16:02:12
也许有没有简单而好的方法,但你应该能够通过声明一个假名称来解决它别名(例如'declare interface __r extends Rectangle {}'并使用别名'__r'而不是全局范围'Rectangle',类似于[Stack Overflow:与接口同名的TypeScript类中使用的方式](http:/ /stackoverflow.com/questions/26591245/typescript-class-with-same-name-as-interface) – xmojmr 2014-12-02 17:00:43