[英]TypeScript 0.9.5: how to define an interface with static members and a class that implements it?
這用於在TypeScript 0.9.1.1中編譯(方法實現省略):
module MyNodule {
export interface ILocalStorage {
SupportsLocalStorage(): boolean;
SaveData(id: string, obj: any): boolean;
LoadData(id: string): any;
}
export class LocalStorage implements ILocalStorage {
static SupportsLocalStorage(): boolean {
return true;
}
static SaveData(id: string, obj: any): boolean {
return true;
}
static LoadData(id: string): any {
return {};
}
}
}
在TypeScript 0.9.5中,我得到編譯器錯誤“Class LocalStorage聲明接口ILocalStorage但不實現它”。
我需要更改什么,以便再次編譯?
注意:在此上下文中使用接口的原因是: - 具有實現的類的文檔 - 讓編譯器檢查接口是否正確實現。
接口定義了類的實例 ,而不是類的實例 。 簡而言之,您無法使用靜態成員實現它。
由於typeScript是結構類型的,因此您可以將類分配給接口。 在這種情況下,類實際上是一個實例:
module MyNodule {
export interface ILocalStorage {
SupportsLocalStorage(): boolean;
SaveData(id: string, obj: any): boolean;
LoadData(id: string): any;
}
export class LocalStorage {
static SupportsLocalStorage(): boolean {
return true;
}
static SaveData(id: string, obj: any): boolean {
return true;
}
static LoadData(id: string): any {
return {};
}
}
var foo : ILocalStorage = LocalStorage; // Will compile fine
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.