这用于在 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
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)