typescript 错误 : Property defined as private on type Class is defined as public on type Interface

标签 typescript

我刚刚在 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)不能有访问修饰符!给了什么?

Typescript playground example

最佳答案

您不能在 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/

相关文章:

node.js - 使用 Typescript 添加一个可能为空值的接口(interface)

angular - 为什么没有在我的自定义 Angular 2 下拉列表中填充 @ContentChildren?

angular - 无法读取 SafeSubscriber._next 未定义的属性 'push'

javascript - 从 Typescript 中的 html 对象提取数据时,如何消除类型错误?

typescript - 你如何部署 Angular 应用程序?

JavaScript 语法错误 : Unexpected end of input

angularjs - 有没有办法让 TypeScript 在客户端的 Plunkr 中转译?

javascript - 错误 TypeError : res. forEach 不是函数

reactjs - 无法在 typescript 中声明 React 上下文?

javascript - 如何使用 javascript 使用 localstorage 在浏览器中存储 Uint8array