Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate TypeScript 2.4 string enums from Java enums #144

Merged
merged 1 commit into from Jun 28, 2017

Conversation

vojtechhabarta
Copy link
Owner

This PR adds possibility to generate TypeScript string enums from Java enums. TypeScript enums were originally number-based but TypeScript 2.4 will allow string members in enums.

Java enum values usually translates to strings in JSON and typescript-generator generates by default union of string literals. For example for this Java enum:

enum Direction {
    Left, Right
}

following TypeScript union type is generated:

type Direction = "Left" | "Right";

This PR adds new possibility to generate following string enum:

export const enum Direction {
    Left = "Left",
    Right = "Right",
}

Configuration

This feature can be used by configuring mapEnum parameter to asEnum value.
For implementation files (.ts) it is also possible to generate enums without const keyword by setting nonConstEnums parameter to true.

Number enums

Some Java enums are rendered as numbers in JSON (for example Jackson2 provides JsonFormat.Shape.NUMBER_INT). Mapping of those enums are not changed by this PR, they are always treated as TypeScript number-based enums.

Deprecated extensions

With this new functionality following extensions are not needed anymore and are deprecated:

  • cz.habarta.typescript.generator.ext.EnumConstantsExtension
  • cz.habarta.typescript.generator.ext.NonConstEnumsExtension

@vojtechhabarta vojtechhabarta merged commit d35af81 into master Jun 28, 2017
@vojtechhabarta vojtechhabarta deleted the string-enums branch June 28, 2017 07:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant