3

I'm trying to build the typescript definition file for this code (in myscript.ts) :

var rectangle = new Rectangle(new Point(20, 20), new Size(60, 60));
var path = new Path.Rectangle(rectangle);
path.strokeColor = 'black';

Please note that here first Rectangle is a different type from the second (Path.Rectangle).

This is what I have for now (in 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
    }
}

With this definition, both of the following lines failed :

var path = new Path.Rectangle(rectangle);
var upperLeft = path.bounds.topLeft;

I understand why but don't know how to fix the definition. Thanks for your help.

4
  • If I were you I'd change the name, not sure there's any other solution. Commented Dec 2, 2014 at 15:33
  • @Omri : I can't change the name, as the code is from an external lib : paperjs. In fact the raw code is from here : paperjs.org/reference/path/#path-rectangle-rectangle Commented Dec 2, 2014 at 16:02
  • 2
    Probably there is no easy and nice way, but you should be able to workaround it by declaring a fake name alias (e.g. declare interface __r extends Rectangle {} and using the alias __r instead of the global scope Rectangle, similar to the way used in Stack Overflow: TypeScript class with same name as interface Commented Dec 2, 2014 at 17:00
  • @xmojmr based on your comment, I found a valid definition, see below. Thanks for your help ! Commented Dec 2, 2014 at 17:32

1 Answer 1

2

Based on @xmojmr comment, I found a valid definition :

My first attempt :

declare class Rectangle {
    constructor(point: Point, size: Size);
    topLeft: Point;
    bottomRight: Point;
}

becomes :

declare class NumericRectangle { // <=================== renamed
    constructor(point: Point, size: Size);
    topLeft: Point;
    bottomRight: Point;
}

declare class Rectangle extends NumericRectangle {  // <=================== added
}

... and

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
    }
}

... becomes :

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
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.