mtennoe / swagger-typescript-codegen

A Swagger Codegenerator tailored for typescript.
Apache License 2.0
141 stars 52 forks source link

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

Open arvind1234 opened 5 years ago

arvind1234 commented 5 years ago

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

mtennoe commented 5 years ago

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 commented 5 years ago

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"
      ]
    }