【问题标题】:TypeScript definition : same name but different types inside and outside module?TypeScript 定义:模块内部和外部名称相同但类型不同?
【发布时间】: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


【解决方案1】:

根据@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
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    • 1970-01-01
    • 2020-11-22
    • 1970-01-01
    • 2014-04-05
    相关资源
    最近更新 更多