2015-02-06 191 views
1

我曾嘗試以下操作:如何從TypeScript中現有的AMD js類繼承到AMD模塊?

class Child { 
    public func() { 
     alert("hello"); 
    } 
    constructor() { 
     Parent.apply(this, arguments); 
    } 
} 
Child["prototype"] = Object.create(Parent.prototype) 
Child["prototype"].constructor = Child; 

但兒童類的實例沒有FUNC()方法。

如何從js類繼承?

回答

4

一般情況下

只是爲了聲明一個現有的類使用declare

declare class Parent { // produces no JS code 
    constructor(val); 
} 
class Child extends Parent { 
    constructor(val) { 
     super(val); 
    } 
} 

外部模塊(AMD)

外部模塊具有案對定義文件.d.ts進行說明。

文件ContainerSurface.d.ts

declare class ContainerSurface { 
    constructor(options); 
} 
export = ContainerSurface; 

如何使用它:

import ContainerSurface = require('ContainerSurface'); 

class Child extends ContainerSurface { 
    constructor(options) { 
    super(options); 
    } 
} 
+0

將是巨大的,如果這個作品!但是,我得到以下錯誤:擴展子類的導出類'子'具有或正在使用私有名稱'父'。任何想法如何告訴編譯器,Parent只是一個非私有的全局變量? – 31415926 2015-02-06 13:51:55

+0

@ 31415926你可以給你的代碼? – Paleo 2015-02-06 14:07:07

+0

我試圖擴展一個AMD模塊https://raw.githubusercontent.com/Famous/famous/develop/src/surfaces/ContainerSurface.js。而且我的Child類也可以編譯生成AMD模塊的選項。如果我將它粘貼到typescriptlang.org遊樂場中,它就可以工作,但不可能在那裏啓用AMD編譯。如果我編譯你的代碼,並說編譯到AMD模塊,它會給出錯誤,如果我添加export = Child;在文件的末尾。 – 31415926 2015-02-06 14:24:03