TypeScript 0.9.5: how to define an interface with static members and a class that implements it?

Used for compilation in TypeScript 0.9.1.1 (implementation of methods omitted):

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 {}; } } 

}

In TypeScript 0.9.5, I get a compiler error "Class LocalStorage declares the ILocalStorage interface but does not implement it."

What do I need to change for it to compile again?

Note: The reason for using the interface in this context was: - to have documentation that the class implements - to check the compiler whether the interface is correctly implemented.

+7
typescript
source share
1 answer

The interface determines which instances of the class will have, and not what the class has. In short, you cannot implement it with static elements.

Since typeScript is structurally typed, you can assign a class to an interface. In this case, the class is actually an instance:

 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 } 
+16
source share

All Articles