Open javibookline opened 1 year ago
This is most likely a duplicate of https://github.com/phillipdupuis/pydantic-to-typescript/issues/15
+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).
@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
Apparently this is possible starting from json-schema-to-typescript v14.1+. Just add the --inferStringEnumKeysFromValues
flag to the json2ts
command line.
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