2015-02-06 69 views
1

我想不會增加其所有成員創造一個大的JavaScript類打字稿的定義,我想它是類型「任何」的。例如:打字稿定義文件,而無須申報所有成員

ContainerSurface.d.ts:

declare class ContainerSurface { 
} 
export = ContainerSurface; 

,只是使用的類並調用它的任何成員,而不需要 「申報」,如:

MyClass.ts:

import ContainerSurface = require('ContainerSurface'); 

class MyClass extends ContainerSurface { 
    constructor(options) { 
     super(options); 
     var a: ContainerSurface({option: "test"}); 
     a.add("test"); 
    } 
} 

export = MyClass; 
+0

爲什麼有一個類,如果你不打算給它一個接口?這幾乎違背了TypeScript的全部內容。 – JLRishe 2015-02-06 18:14:40

+0

如果你想使用一個JavaScript庫(在我們的例子中是famo.us),但沒有時間和知識爲它生成.d.ts文件,並且需要繼承使用來自JavaScript類的typescript, ...相信我... – 2015-02-06 22:01:36

回答

1

你可以做到這一點使用下列內容:

declare var ContainerSurface: any; 

export = ContainerSurface; 

這是我的Definition Files Made Easy過程中的第一步 - 所以您可以逐漸添加類型,以此爲起點。

第二步是鬆散地指定的屬性和方法:

declare class ContainerSurface { 
    myMethod: any; 
    myProperty: any; 
} 

export = ContainerSurface; 

抓住任何速勝,如基本類型,並且只需添加你實際使用,開始了的東西。

+0

這對我來說不起作用......我使用的是AMD模塊,難道這是這個問題嗎?它適用於我描述的方式,但我必須包含所有我稱之爲的功能。使用你的方法,我得到的錯誤:「超級只能在派生類中引用」,「擴展的導出類'...'已經或正在使用私有名稱'...' – 2015-02-06 22:12:33

+0

你不能擴展任何如果你想使用繼承,你必須提供更多的類型信息,但是你可以按照我鏈接的文章逐步完成。 – Fenton 2015-02-06 22:38:06

+0

我在當前的代碼庫中做了這個,我聲明:declare class ContainerSurface {prototype( opts:any); add(s:string);} export = ContainerSurface;但是如果有一整套其他函數,但我不想聲明它們(例如del(),prev(), next()),是否有辦法做到這一點,還是應該聲明我實際使用的所有函數? – 2015-02-07 00:19:30