Feature request: Emit string enums instead of string literal unions
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.
Hey @rkdrnf! When I released JSTT, TS didn't support string enums yet :) We should support them behind an option. PRs are welcome!
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.
{ "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?
@smil2k This works great, thanks. I created a PR to document this method to create a string enum: #306.
This can be closed as #306 was merged.
@bcherny @johnbillion Would you guys not be in favour to make this possible without adding custom schema properties (https://github.com/bcherny/json-schema-to-typescript/pull/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? https://github.com/bcherny/json-schema-to-typescript/pull/262 And if it was, then the flag is not mentioned in the options?
Reopening this per @PerttiK's request.
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
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.