user1766721
user1766721

Reputation: 31

typescript Base type 'xxxx' is referenced before its declaration

I'm trying to make a TypeScript declaration file for CreateJS ( www.createjs.com ).

I have a base class declaration which looks like this:

/// <reference path="../geom/Point.d.ts"/>
/// <reference path="../geom/Rectangle.d.ts"/>
/// <reference path="../geom/Matrix2D.d.ts"/>
/// <reference path="../filters/Filter.d.ts"/>
/// <reference path="Shadow.d.ts"/>
/// <reference path="Shape.d.ts"/>
/// <reference path="Stage.d.ts"/>

declare module createjs {
  class DisplayObject {

    // constructor:
    constructor();

    ... additional methods

  }
}

and then I declare more classes derived from DisplayObject:

/// <reference path="DisplayObject.d.ts"/>
declare module createjs {
      class Bitmap extends DisplayObject{
        constructor (imageOrUri:any);

        ... additional methods

      }
    }


   /// <reference path="DisplayObject.d.ts"/>
   declare module createjs {
      class Container extends DisplayObject{
        constructor();

        ... additional methods

      }
    }

    /// <reference path="../../tweenjs/Timeline.d.ts"/>
    /// <reference path="DisplayObject.d.ts"/>
    /// <reference path="Container.d.ts"/>
    declare module createjs {
      class MovieClip extends Container{
        constructor (mode: string, startPosition: number, loop: bool, labels: any);

        ... additional methods

      }
    }

etc. etc.

But I'm always getting compiler errors like this:

Base type 'DisplayObject' is referenced before its declaration or Base type 'Container' is referenced before its declaration.

I don't see what I'm doing wrong, everything looks ok. Any help really appreciated ... many thanks in advance!

Upvotes: 3

Views: 2316

Answers (1)

Fenton
Fenton

Reputation: 250922

If your code is split across multiple files you'll need to let the development tools and the compiler know about the other files using a reference:

/// <reference path="createjs.ts" />

I have just tested this in Visual Studio and it seems to clear the error, assuming your first code block is in a file called createjs.ts, you would add this to the top of your second file.

If they are all in the same file in the order your specified, it all works for me.

Update

I have created a Visual Studio project with the following files and it all works - one thing that occurred to me is that you possibly have your code organised into folders, in which case you need to use relative paths such as:

/// <reference path="./createjs/DisplayObject.d.ts" />

Anyway, here is my working set up in an empty project:

DisplayObject.d.ts

declare module createjs {
    class DisplayObject {
        // constructor:
        constructor ();
    }
}

Bitmap.d.ts

/// <reference path="DisplayObject.d.ts"/>

declare module createjs {
    class Bitmap extends DisplayObject {
        constructor (imageOrUri: any);
    }
}

Container.d.ts

/// <reference path="DisplayObject.d.ts"/>

declare module createjs {
    class Container extends DisplayObject {
        constructor ();
    }
}

And all is happy. You don't need the export keyword, because they are all in the same module.

I'm happy to zip this up somewhere if you want to view the example.

Upvotes: 5

Related Questions