2017-07-21 58 views
-4

我必須指定應該在課堂上實現的界面中的所有操作。如何在TypeScript中定義類的接口?

我從PHP轉移到TypeScript。在PHP

創建界面非常簡單:

interface iTemplate 
{ 
    public function move($name, $var); 

} 

類是:

Class Mover inmpelments iTemplate { 
    function move($name, $var){} 
} 

如何做到這一點的打字稿?例如,我有類用戶,它可以:

edit profile 
see users 
etc 
+4

語法類似於*,但*我建議你開始一個教程或一本書或[打字稿網站(https://開頭WWW .typescriptlang.org/docs/home.html),這是非常有用的。這比在SO上提出一個問題要好得多,因爲你會遇到用新語言編程的每個語法細微差別。 – Igor

+0

我已經找到僅用於屬性的接口:不用於功能:'接口Person {姓名}字符串; 姓氏:字符串; }' – OPV

回答

1
interface MyInterface { 
    editProfile(profileId: number): void; 
    seeUsers(): object[]; 
    etc: string; 
} 

class MyImplementation implements MyInterface{ 
    editProfile(profileId: number): void { 
     throw 'todo'; 
    } 

    seeUsers(): object[] { 
     throw 'todo'; 
    } 

    readonly etc = 'something else'; 
} 
相關問題