json-schema-to-typescript icon indicating copy to clipboard operation
json-schema-to-typescript copied to clipboard

Feature request: Emit string enums instead of string literal unions

Open rkdrnf opened this issue 7 years ago • 11 comments

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.

rkdrnf avatar Nov 13 '18 03:11 rkdrnf

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

bcherny avatar Nov 27 '18 06:11 bcherny

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.

cdietschrun avatar Oct 15 '19 18:10 cdietschrun

{ "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 avatar Jan 18 '20 15:01 smil2k

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

johnbillion avatar May 23 '20 16:05 johnbillion

This can be closed as #306 was merged.

johnbillion avatar Dec 06 '20 17:12 johnbillion

@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?

PerttiK avatar Dec 17 '20 21:12 PerttiK

Reopening this per @PerttiK's request.

bcherny avatar Dec 30 '20 22:12 bcherny

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

amh4r avatar Sep 06 '21 21:09 amh4r

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.

samkearney avatar Aug 25 '22 19:08 samkearney