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

时间:2017-06-14 20:07:35

标签: java typescript enums

我在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吗?

1 个答案:

答案 0 :(得分:0)

我不确定您是否可以使用Typescript枚举来做到这一点,但应该遵循以下方法:

services.AddSingleton<MessageAttribute>();

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

相关问题