2012-10-05 37 views
7

我正在尝试为KineticJS库创建.d.ts文件。到目前为止,我已经创建了以下接口声明“kinect.d.ts”(我冒出了一个代码为位计算器,但我希望你的想法)如何在TypeScript中创建环境类声明

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

我希望这将是足以能够建立在我的app.ts一个Kinetic.Rect对象文件

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

但现在看来,我必须做一些额外的工作,打字稿使用KineticJS类(如矩形)。任何人都可以提供一些关于如何将其归档的提示吗?

回答

6

你看的打字稿例如应用在:http://typescript.codeplex.com/SourceControl/changeset/view/fe3bc0bfce1f#samples/imageboard/mongodb.ts

的代码在此链接创建MongoDB的库的定义。这与Sohnee的答案之间的一个区别是,Sohnee实现了构造函数,与以下代码片段相比,它是一个存根类的链接。我没有足够的信誉在接受的答案中询问Sohnee为什么他实现了一个环境类的构造函数?

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); 
+0

这看起来很有希望,今晚我会看看它 –

+0

是的,这是它!这是我正在寻找的。现在我可以编写'var server = new mongodb.Server(“”,1,“something”,“”);'而不必转换为正确的类型。 –

5

这是给你动力学类创建环境定义我的职业例如:

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

请注意,我用declare var Kinetic: IKinetic;告诉打字稿那珂是特定类型的。

更新 - 示例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   
}); 
+0

嗨Sohnee,你的例子似乎工作,但我非常希望返回类型矩形(或如你的例子中的Shape类型)。这样Intellisense显示返回类型的所有方法。我尝试添加:形状为您的示例 interface IKinetic Rect(shape:Shape); } 但我得到的编译器错误“新的表达式只对构造函数有效” –

+0

也thx您的建议 –

+0

我将添加返回类型到接口为您... – Fenton

0

ITodoStorage真是接口,TodoStorage是实现,但我不希望定义类,因为这将迫使我实现所有成员。相反,我也制作TodoStorage界面。最后,我将var声明为带new关键字的构造函数。

declare interface ITodoStorage { 
    get_todos() : TodoItem[]; 
    set_todos(value : TodoItem[]) : void; 
} 

declare interface TodoStorage extends ITodoStorage { 
} 

declare var TodoStorage : { 
    new(): TodoStorage; 
} 

然后我能够调用构造函数

var storageService : ITodoStorage = new TodoStorage(); 

不幸的是,变种的隐藏TodoStorage类型。

0

我意识到这是现在老了,但你可以找到一个完整的文件kinetic.d.ts这里:http://kineticjstypescript.codeplex.com/

+1

呵呵是的,这是我解决我的问题后所作的定义:) –