简体   繁体   English

如何在打字稿中声明公共枚举?

[英]How do I declare a public enum in typescript?

For the following class: 对于以下课程:

module LayoutEngine {

    enum DocumentFormat {
        DOCX = 1
    };

    export class DocHeader {

        public format : DocumentFormat;
    }
}

I have two questions: 我有两个问题:

  1. The above has a compile error where it says "Public property 'format' of exported class has or is using private type 'DocumentFormat'." 上面有一个编译错误,它说“导出类的公共属性'格式'已经或正在使用私有类型'DocumentFormat'。” but a declaration of public before the enum is also an error. 但在枚举之前公开声明也是一个错误。 So how do I do this? 那我该怎么做?
  2. Is there a way to place the enum declaration inside the class? 有没有办法将枚举声明放在类中? Just a module name isn't great for namespacing as I have a lot of classes in that module. 只是一个模块名称对于命名空间来说不是很好,因为我在该模块中有很多类。

thanks - dave 谢谢 - 戴夫

The above has a compile error where it says "Public property 'format' of exported class has or is using private type 'DocumentFormat'. 上面有一个编译错误,它说“导出类的公共属性'格式'已经或正在使用私有类型'DocumentFormat'。

Simply export : 只需出口:

module LayoutEngine {

    export enum DocumentFormat {
        DOCX = 1
    };

    export class DocHeader {

        public format : DocumentFormat;
    }
}

Is there a way to place the enum declaration inside the class? 有没有办法将枚举声明放在类中?

the enum typescript type needs to be at a module level (a file or inside a module). enum打字稿类型需要在模块级别(文件或模块内)。 Of course if you want it inside the class just use a json object 当然,如果你想在类中使用json对象

module LayoutEngine {
    export class DocHeader {
        DocumentFormat = {
            DOCX: 1
        };

        public format : number;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM