[英]How to convert a Java Enum to Typescript Enum
I have an Enum in Java set up like so: 我有一个像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;
}
}
and I want to set up an equivalent Typescript Enum and I've done so like this: 我想设置一个等效的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";
}
}
}
My question is, can I create a constructor function which has a description and sqlType in Typescript as well? 我的问题是,我可以创建一个构造函数函数,它在Typescript中也有一个描述和sqlType吗?
I'm not entirely sure if you can do that with typescript enums, but something along these lines should work: 我不完全确定你是否可以使用打字稿枚举来做到这一点,但这些内容应该有效:
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;
}
}
From what I can tell, typescript enums basically translate to numbers, so this may be the only way. 据我所知,打字稿枚举基本上转换为数字,所以这可能是唯一的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.