Skip to content

Add Support for Typescript Enum & Union Generation#91

Merged
gzuidhof merged 7 commits into
gzuidhof:mainfrom
PY44N:main
Nov 19, 2025
Merged

Add Support for Typescript Enum & Union Generation#91
gzuidhof merged 7 commits into
gzuidhof:mainfrom
PY44N:main

Conversation

@PY44N

@PY44N PY44N commented Jul 28, 2025

Copy link
Copy Markdown
Contributor

Original:

export type UserRole = string;
export const UserRoleDefault: UserRole = "viewer";
export const UserRoleEditor: UserRole = "editor"; // Line comments are also kept

With config enum_style: "enum":

export enum UserRole {
  Default = "viewer",
  Editor = "editor", // Line comments are also kept
}

With config enum_style:"union"

export type UserRole = "viewer" | "editor";

@stavros-k

Copy link
Copy Markdown

Not a maintainer, but thankful for this work.
Would it be possible to have enum as untion type option as well?

@PY44N

PY44N commented Aug 11, 2025

Copy link
Copy Markdown
Contributor Author

I haven't used Typescript unions much, but it doesn't seem like that would be too bad

@PY44N PY44N changed the title Add Support for Typescript Enum Generation Add Support for Typescript Enum & Union Generation Aug 11, 2025
@gzuidhof

Copy link
Copy Markdown
Owner

Thank you for this PR, it's really great :)

One comment on the generated code in the union case, I would propose to keep the constants around (so that Typescript code can reference it).

Basic enum generation with enum_style: "union"

enum_style: "union"
type UserRole = string
const (
    UserRoleDefault UserRole = "viewer"
    UserRoleEditor  UserRole = "editor"
)
export type UserRole = typeof UserRoleDefault | typeof UserRoleEditor;

export const UserRoleDefault: UserRole = "viewer"
export const UserRoleEditor: UserRole = "editor"

(Or this can be another variant.. But I would say it has benefits over only having UserRole = "viewer" | "editor", as it doesn't provide a way to access the individual values (and their comments in docstring).

@PY44N

PY44N commented Oct 1, 2025

Copy link
Copy Markdown
Contributor Author

Thank you for this PR, it's really great :)

One comment on the generated code in the union case, I would propose to keep the constants around (so that Typescript code can reference it).

Basic enum generation with enum_style: "union"

enum_style: "union"
type UserRole = string
const (
    UserRoleDefault UserRole = "viewer"
    UserRoleEditor  UserRole = "editor"
)
export type UserRole = typeof UserRoleDefault | typeof UserRoleEditor;

export const UserRoleDefault: UserRole = "viewer"
export const UserRoleEditor: UserRole = "editor"

(Or this can be another variant.. But I would say it has benefits over only having UserRole = "viewer" | "editor", as it doesn't provide a way to access the individual values (and their comments in docstring).

Yeah, that makes sense. I just pushed 10d4c17 to address this change

@akleiber

Copy link
Copy Markdown

Would be nice to see a review here.
Thank you PY44N for the PR! ❤️

@gzuidhof gzuidhof left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great :) Thank you!

Sorry for not merging earlier (I somehow forgot about it after going away on holiday..)

@gzuidhof gzuidhof merged commit 4032e1a into gzuidhof:main Nov 19, 2025
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.

4 participants