作为私有或受保护的对象

Typescript - object as a private or protected

本文关键字:受保护 对象      更新时间:2023-09-26

如何在Typescript中定义一个对象为protected或private ?

我不能在接口中这样做(只有公共允许),我也不能在类中这样做,因为下面不起作用。

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

有什么提示吗?谢谢你

Zoltán Tamá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
};

如果您在module中定义了一个类或接口,您可以使用export关键字来选择是否要export它。

如果一个类或接口没有导出,那么它只在声明的模块中可见。

但是,如果在导出类的任何公共成员、方法等中使用了未导出的类或接口,则会得到编译错误,然后必须导出它。