I am trying to use typescript-generator to generate a Typescript enum from a Java enum.
My example Java enum:
public enum DefinedFields {
DEFINITION("definition"),
HAS_DIRECT_CHILDREN("hasDirectChildren");
private final String text;
DefinedFields(String text) {
this.text = text;
}
public String getText() {
return text;
}
}
Here is the relevant bit from my Maven pom:
<build>
<plugins>
<plugin>
<groupId>cz.habarta.typescript-generator</groupId>
<artifactId>typescript-generator-maven-plugin</artifactId>
<version>3.2.1263</version>
<executions>
<execution>
<id>generate</id>
<goals>
<goal>generate</goal>
</goals>
<phase>process-classes</phase>
</execution>
</executions>
<configuration>
<jsonLibrary>gson</jsonLibrary>
<mapEnum>asEnum</mapEnum>
<classes>
<class>DefinedFields</class>
</classes>
<outputKind>module</outputKind>
</configuration>
</plugin>
</plugins>
</build>
When I run Maven, the following is generated in DefinedFields.d.ts
:
export const enum DefinedFields {
DEFINITION = "DEFINITION",
HAS_DIRECT_CHILDREN = "HAS_DIRECT_CHILDREN",
}
However, what I need to be generated is:
export const enum DefinedFields {
DEFINITION = "definition",
HAS_DIRECT_CHILDREN = "hasDirectChildren"
}
That is the enum values need to correspond with the Java text
field.
Some questions you may have
-
Why not just recreate the enum manually in Typescript? We have a lot of enum values and it will really help reduce errors if we can define it once.
-
The values of the
text
field are non-negotiable. I.e., we cannot change it to correspond with the generated Typescript values.