我刚刚在 TypeScript 0.9.5 中开始了一个新项目,下面的代码抛出了一个错误:
类服务声明了 IService 但没有实现它。在服务类型上定义为私有(private)的属性“getUserInfo”在 IService 类型上定义为公共(public)
module App.Interfaces {
export interface IService {
getUserInfo(): void;
}
}
module App.Services {
export class Service implements App.Interfaces.IService {
private getUserInfo(): void { }
}
}
自从我使用 TypeScript 以来,我就知道接口(interface)不能有访问修饰符!给了什么?
最佳答案
您不能在 Service
类的 getUserInfo
函数上使用 private
访问修饰符,因为它是在接口(interface) 上声明的我服务
。
如果类是 IService
,它需要公开声明接口(interface)的所有函数/属性。
module App.Services {
export class Service implements App.Interfaces.IService {
/* private <= remove */ getUserInfo(): void { }
}
}
关于 typescript 错误 : Property defined as private on type Class is defined as public on type Interface,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21356630/