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

时间:2014-04-14 23:35:15

标签: enums typescript

以下课程:

module LayoutEngine {

    enum DocumentFormat {
        DOCX = 1
    };

    export class DocHeader {

        public format : DocumentFormat;
    }
}

我有两个问题:

  1. 上面有一个编译错误,上面写着“公共财产” 导出类的“格式”具有或正在使用私有类型 'DocumentFormat'。“但是在enum之前公开声明 也是一个错误。那我该怎么做呢?
  2. 有没有办法将枚举声明放在类中?只是一个模块名称对于命名空间来说不是很好,因为我在该模块中有很多类。
  3. 谢谢 - 戴夫

1 个答案:

答案 0 :(得分:34)

  

上面有一个编译错误,其中显示"公共财产'格式'导出的类已经或正在使用私有类型' DocumentFormat'。

简单地导出:

module LayoutEngine {

    export enum DocumentFormat {
        DOCX = 1
    };

    export class DocHeader {

        public format : DocumentFormat;
    }
}
  

有没有办法将枚举声明放在类中?

enum typescript类型需要在模块级别(文件或模块内)。当然,如果你想在课堂上使用json对象

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

        public format : number;
    }
}