ChrisCa
ChrisCa

Reputation: 11056

Reference other libraries

I would like to be able to reference an external library (EaselJs) from within the constructor of one of my classes:

export class GameCanvas {

        private stage; 
        constructor() {
            this.stage = new createjs.Stage("canvasElementId"); // this doesn't compile
        }

        start(delay, callback) {
        }
    }

This will not compile as it doesn't know what createjs is

I could pass it in to the constructor. But this object is quite a way down an object graph so it would need to come via lots of other calls

Is there another way I can satisfy the Typescript compiler and make it know about objects in external libraries

thanks

Upvotes: 0

Views: 543

Answers (3)

andyks
andyks

Reputation: 478

In terms of providing 3rd party references one feature worth knowing is that if you include something like the following line as the first line of your ts file:

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

where references.ts includes references to specific other ts files the location of the files specified in references.ts is relative to the location of references.ts and not the ts file which includes the above line.

This is helpful if you want to play around with switching implementations and the like, say one version of code obtained from DT doesn't work with a specific version of jQuery or Knockout for instance.

Upvotes: 0

Ondrej Svejdar
Ondrej Svejdar

Reputation: 22094

I use references from DefinetelyTyped project (they declare just interface, that satisfies compiler).

https://github.com/borisyankov/DefinitelyTyped/tree/master/easeljs

Upvotes: 0

basarat
basarat

Reputation: 276313

Quick / Dirty solution : create a declaration for createjs:

declare var createjs;
export class GameCanvas {

        private stage; 
        constructor() {
            this.stage = new createjs.Stage("canvasElementId"); // this doesn't compile
        }

        start(delay, callback) {
        }
    }

Better solution. Use the declarations created by the community for you : https://github.com/borisyankov/DefinitelyTyped

For EaselJS: https://github.com/borisyankov/DefinitelyTyped/blob/master/easeljs/easeljs.d.ts

Sample usage: https://github.com/borisyankov/DefinitelyTyped/blob/master/easeljs/easeljs-tests.ts

Upvotes: 2

Related Questions