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: Emit string enums instead of string literal unions #200

Closed
rkdrnf opened this issue Nov 13, 2018 · 12 comments
Closed

Feature request: Emit string enums instead of string literal unions #200

rkdrnf opened this issue Nov 13, 2018 · 12 comments

Comments

@rkdrnf
Copy link

rkdrnf commented Nov 13, 2018

I hope enums of string

{
    "title": "TestEnum",
   "type": "string",
    "enum": [
         "A",
         "B"
    ]
}

to be converted as typescript string enum like below.

enum TestEnum {
A = "A",
B = "B"
}

Is there any way to do this?

I need this because I want to reference this enum in other json file as a string,
and also in code, I don't want to use raw string to reference these enum values.

@bcherny
Copy link
Owner

bcherny commented Nov 27, 2018

Hey @rkdrnf! When I released JSTT, TS didn't support string enums yet :) We should support them behind an option. PRs are welcome!

@cdietschrun
Copy link

Is this still not possible? I was really hoping it would be and have run into this now in using it. I may make a PR to do this if I have the chance.

@smil2k
Copy link

smil2k commented Jan 18, 2020

{
"type": "object",
"properties": {
"id": {
"type": "string",
"example": "3445344367kj"
},
"ReservationKind": {
"enum": ["DateOnly", "Exact"],
"tsEnumNames": ["DateOnly", "Exact"]
},
"name": {
"type": "string"
}
},
"required": ["ReservationKind", "name"]
}

Creates:

export interface Resource {
id?: string;
ReservationKind: ReservationKind;
name: string;
[k: string]: any;
}

export const enum ReservationKind {
DateOnly = "DateOnly",
Exact = "Exact"
}

This is what you need, right?

@johnbillion
Copy link
Contributor

@smil2k This works great, thanks. I created a PR to document this method to create a string enum: #306.

@johnbillion
Copy link
Contributor

This can be closed as #306 was merged.

@bcherny bcherny closed this as completed Dec 6, 2020
@PerttiK
Copy link

PerttiK commented Dec 17, 2020

@bcherny @johnbillion Would you guys not be in favour to make this possible without adding custom schema properties (#306) and having a flag instead?
That flag would indicate whether you want the default behaviour
ReservationKind: "DateOnly" | "Exact"
or whether you want
ReservationKind: ReservationKind with ReservationKind an enum type exported along the interface

That's exactly what @cdietschrun seems to have done but it was never finally merged? #262
And if it was, then the flag is not mentioned in the options?

@bcherny bcherny changed the title String enum instead of string literal? Feature request: Emit string enums instead of string literal unions Dec 30, 2020
@bcherny bcherny reopened this Dec 30, 2020
@bcherny
Copy link
Owner

bcherny commented Dec 30, 2020

Reopening this per @PerttiK's request.

@amh4r
Copy link

amh4r commented Sep 6, 2021

I've opened a PR (#405) to add x-enum-varnames support. The x-enum-varnames custom extension is used by other OpenAPI generators, and seems to serve the same purpose as tsEnumNames

@samkearney
Copy link

Hi, just adding my support for this feature request. I think the x-enum-varnames approach is valid for people who can add custom properties to their schemas, but I cannot currently do that so I'd prefer the solution in PR #262.

@viplmad
Copy link

viplmad commented Nov 29, 2023

Hi, I made a PR (#556) about a month ago based on a closed PR (#262) that implemented a solution for this. Is there any way it could be reviewed and eventually merged?

@codan84
Copy link
Contributor

codan84 commented Feb 7, 2024

My PR: #578
Addresses requests made in the original PR: #262 (review)

@bcherny
Copy link
Owner

bcherny commented Jun 25, 2024

This is included in [email protected]. Thank you @codan84 for the contribution!

@bcherny bcherny closed this as completed Jun 25, 2024
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

10 participants