phillipdupuis / pydantic-to-typescript

CLI Tool for converting pydantic models into typescript definitions
MIT License
285 stars 48 forks source link

Enum names are not preserved #30

Open javibookline opened 1 year ago

javibookline commented 1 year ago

Enums which are part of a pydantic model are translated to a type, losing relevant and useful information that could be kept if they were translated to enums in typescript too

robbieaverill commented 1 year ago

This is most likely a duplicate of https://github.com/phillipdupuis/pydantic-to-typescript/issues/15

kevinhikaruevans commented 1 year ago

+1.

This would be nice considering I'm using enum names in backend and I would prefer not to use just the enum values in my frontend (which are nearly meaningless without the names).

guaycuru commented 1 month ago

@robbieaverill I don't think this is a duplicate of #15

Currently it works like this, it converts:

class Status(str, Enum):
    active = "active"
    inactive = "inactive"

class SomeSchema(BaseSchema):
    id: UUID
    status: Status

To:

export type Status = "active" | "inactive";

export interface SomeSchema {
  id: string;
  status: Status;
}

Which means we can't use Status.active in our TS code. But if instead it was converted like this:

export enum Status {
    active = "active",
    inactive = "inactive",
}

export interface SomeSchema {
  id: string;
  status: Status;
}

We could use Status.active in TS code

guaycuru commented 1 month ago

Apparently this is possible starting from json-schema-to-typescript v14.1+. Just add the --inferStringEnumKeysFromValues flag to the json2ts command line.