2012-10-05 36 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

呵呵是的,這是我解決我的問題後所作的定義:) –