Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create Ambient Class Declarations in TypeScript

Tags:

typescript

I am trying to create a .d.ts file for the KineticJS library. So far I have created the following interface declaration "kinect.d.ts".(I cropped the code a bit for stackoverflow but I hope you get the idea)

module Kinetic {

    interface Rect extends  Shape {
        constructor (config) ;
    }

    interface Shape extends Node
    { 

    }

    interface Node {
        constructor (config);
        clone(attrs): Node;
        getAbsoluteOpacity(): number;
        getAbsolutePosition(): any;       

        /*
        other methods removed for stackoverflow example
        */
    }
}

I hoped this would be enough to be able to create a Kinetic.Rect object in my app.ts file

/// <reference path="Kinetic.d.ts" />
var rect = new Kinetic.Rect({
          x: 239,
          y: 75,
          width: 100,
          height: 50        
        });

But it appears I have to do some extra work to use the KineticJS classes (like Rect) in TypeScript. Could anyone give some pointers on how to archive this?

like image 279
Ralph de Ruijter Avatar asked Oct 05 '12 10:10

Ralph de Ruijter


People also ask

What is ambient declaration in TypeScript?

Ambient declarations are a way of telling the TypeScript compiler that the actual source code exists elsewhere. When you are consuming a bunch of third party js libraries like jquery/angularjs/nodejs you can't rewrite it in TypeScript.

What are ambient modules TypeScript?

Ambient modules is a TypeScript feature that allows importing libraries written in JavaScript and using them seamlessly and safely as if they were written in TypeScript. An ambient declaration file is a file that describes the module's type but doesn't contain its implementation.

How do I use TypeScript to declare?

declare keyword directly integrate these libraries in our code and decrease the chance of error in our TypeScript code. We use declare keyword only in Ambient file to use the libraries method and variables. Syntax: declare var Variable_Name; declare module Name_of_Module{// Body of module };


2 Answers

Have you looked at the TypeScript example app at: http://typescript.codeplex.com/SourceControl/changeset/view/fe3bc0bfce1f#samples/imageboard/mongodb.ts

The code at this link creates a definition for the mongodb library. One difference between this and the Sohnee answer is that Sohnee implements the constructor in contrast to the following code snip from the link which is a stub class. I do not have enough reputation to ask Sohnee in the accepted answer why he implemented the constructor for an ambient class?

declare module "mongodb" {
   export class Server {
       constructor(host: string, port: number, opts?: any, moreopts?: any);
   }
   export class Db {
       constructor(databaseName: string, serverConfig: Server);
       public open(callback: ()=>void);
like image 145
camelCase Avatar answered Oct 19 '22 21:10

camelCase


Here is my working example of creating ambient definitions for your Kinetic class:

interface Shape {
    x: number;
    y: number;
    width: number;
    height: number;
}

interface IKinetic {
    Rect(shape: Shape);
}

declare var Kinetic: IKinetic;

var rect = <Shape> new Kinetic.Rect({
  x: 239,
  y: 75,
  width: 100,
  height: 50        
});

Note that I have used declare var Kinetic: IKinetic; to tell TypeScript that Kinetic is of the particular type.

Update - Example 2

interface IShape {
    x: number;
    y: number;
    width: number;
    height: number;
}

interface IRect extends IShape {

}

module Kinetic {
    export class Rect implements IRect {
        public x: number;
        public y: number;
        public width: number;
        public height: number;
        constructor(rect: IShape) {
            this.x = rect.x;
            this.y = rect.y;
            this.width = rect.width;
            this.height = rect.height;
        }
    }
}

var rect = new Kinetic.Rect({
  x: 239,
  y: 75,
  width: 100,
  height: 50        
});
like image 24
Fenton Avatar answered Oct 19 '22 22:10

Fenton