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

[Feature request] Map enums to string enums instead of string literal type #76

Open
arvind1234 opened this issue Jun 4, 2019 · 2 comments

Comments

@arvind1234
Copy link

Is there a way to transform enums into tyepscript string enums instead of string literal type?

@mtennoe
Copy link
Owner

mtennoe commented Jun 5, 2019

That might be possible to add support for. One of the current challenges though is that based on the swagger schema (AFAIK) we dont get a name of the enum type, so that needs to be generated somehow when generating the output TS types (so that we have a type to reference), while ensuring the new type name is unique (and at least to some degree with a descriptive name). Have you tried marking the enum type as a reference type in the schema? If that is done that type definition can be used for this, making it easier to make it a string enum type. It could then be possible to have an option flag for which enum type to generate (we want to retain backwards compatibility, and I know many prefer string union types over string enums)

What is string enums needed for btw? Is it needed to have a declared type that can be referenced, or is it something specific with the string enum types thats needed, or is it something else?

@scottc
Copy link

scottc commented Jun 7, 2019

I know many prefer string union types over string enums

I generally prefer string unions.

Unless the name is different from the value. And has some additional semantic meaning, that would increase code clarity.

I'm using NSwag C# server, and it generates some custom x-enumNames field.

    "SomeType": {
      "type": "string",
      "description": "",
      "x-enumNames": [
        "One",
        "Two",
        "Three"
      ],
      "enum": [
        "1",
        "2",
        "3"
      ]
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants