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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23072208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:13:15  来源:igfitidea点击:

How do I declare a public enum in typescript?

enumstypescript

提问by David Thielen

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'." 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.
  1. 上面有一个编译错误,它说“导出类的公共属性‘格式’具有或正在使用私有类型‘文档格式’。” 但是在枚举之前声明 public 也是一个错误。那么我该怎么做呢?
  2. 有没有办法将枚举声明放在类中?仅仅一个模块名称并不适合命名空间,因为我在该模块中有很多类。

thanks - dave

谢谢 - 戴夫

回答by basarat

The above has a compile error where it says "Public property 'format' of exported class has or is using private type '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 enumtypescript type needs to be at a module level (a file or inside a module). Of course if you want it inside the class just use a json object

所述enum打字稿类型需要处于一个模块级(文件或模块内)。当然,如果您想在类中使用它,只需使用 json 对象

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

        public format : number;
    }
}