Typescript - 对象作为私有或受保护

时间:2015-10-20 07:46:12

标签: javascript interface typescript private protected

如何在Typescript中将对象定义为受保护或私有?

我不能在界面中做到(只允许公开), 我也不能在课堂上这样做,因为下面不起作用。

private options : interface{
    collapsible  : boolean;
    collapsed    : boolean;
    editable     : boolean;
}

任何提示? 谢谢

ZoltánTamási - 谢谢你的帮助,

在界面

declare module ICoreModule{
    // protected or private
    interface IOptions{
        initWhenDataReady   : boolean;
        collapsible         : boolean;
        collapsed           : boolean;
        editable            : boolean;
    }

    export interface ICoreScope extends ng.IScope{
        sandboxSave : Function;
        data        : Object;
    }

    export interface Class extends App.Directive{
        $scope   : ICoreScope;
        $element : ng.IRootElementService;
        $attr    : ICoreAttr;
        $ctrl    : ng.IFormController;
    }
}

在课堂上:

protected options : ICoreModule.IOptions = <ICoreModule.IOptions>{
    initWhenDataReady   : true,
    collapsible         : true,
    collapsed           : true,
    editable            : true
};

1 个答案:

答案 0 :(得分:1)

如果您在module中定义了一个类或一个intertace,您可以使用export关键字选择是否export

如果没有对类或接口进行exproted,那么它只在声明模块中可见。

但是,如果在导出的类的任何公共成员,方法等中使用了非导出的类或接口,那么您将收到编译错误,然后您必须将其导出。