繁体   English   中英

如何将Java枚举转换为Typescript枚举

[英]How to convert a Java Enum to Typescript Enum

我有一个像Java一样的Enum设置:

public enum ObjectType implements Serializable{

    TABLE(0,"TABLE"),
    VIEW(1,"VIEW");

    private int objectType;
    private String description; 
    private String sqlType;

    ObjectType(int objectType, String description, String sqlType) {
        this.objectType = objectType;
        this.description = description;
        this.sqlType = sqlType;
    }
}

我想设置一个等效的Typescript Enum,我这样做了:

export enum ObjectType {
    TABLE,
    VIEW
}

export namespace ObjectType {

    export function getType(description : string): ObjectType {
        if(description.toUpperCase() === "TABLE") {
            return ObjectType.TABLE;
        } else if(description.toUpperCase() === "VIEW") {
            return ObjectType.VIEW;
        } else {
            return null;
        }
    }

    export function getObjectType(objectType: ObjectType): string {
        switch(objectType) {
            case ObjectType.TABLE:
                return "TABLE";
            case ObjectType.VIEW:
                return "VIEW";
        }
    }
}

我的问题是,我可以创建一个构造函数函数,它在Typescript中也有一个描述和sqlType吗?

我不完全确定你是否可以使用打字稿枚举来做到这一点,但这些内容应该有效:

class MyEnum{

  public static readonly ONE=new MyEnum(1, "Single-Digit");
  public static readonly TWO=new MyEnum(200, "Multi-Digit");

  public readonly index: number
  public readonly type: string

  private constructor(index:number, type: string){
    this.index=index;
    this.type=type;
  }
}

据我所知,打字稿枚举基本上转换为数字,所以这可能是唯一的方法。

暂无
暂无

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

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