【问题标题】:How do I declare a public enum in typescript?如何在打字稿中声明公共枚举?
【发布时间】:2014-05-29 03:04:29
【问题描述】:

对于以下课程:

module LayoutEngine {

    enum DocumentFormat {
        DOCX = 1
    };

    export class DocHeader {

        public format : DocumentFormat;
    }
}

我有两个问题:

  1. 上面有一个编译错误,上面写着“公共属性 导出类的“格式”已经或正在使用私有类型 'DocumentFormat'。”但在枚举之前的公共声明是 也是一个错误。那么我该怎么做呢?
  2. 有没有办法将枚举声明放在类中?只是一个模块名称并不适合命名空间,因为我在该模块中有很多类。

谢谢 - 戴夫

【问题讨论】:

    标签: enums typescript


    【解决方案1】:

    上面有一个编译错误,上面写着“导出类的公共属性'格式'已经或正在使用私有类型'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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-10
      • 1970-01-01
      • 1970-01-01
      • 2020-04-30
      • 2020-01-29
      • 2018-06-12
      • 2018-10-22
      • 2020-01-17
      相关资源
      最近更新 更多