phillipdupuis / pydantic-to-typescript

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

Convert standalone enum #15

Closed C-Powers closed 2 years ago

C-Powers commented 2 years ago

Hey there, this package is really great, thanks so much!

It seems as though I'm not able to convert a standalone enum.

For example, I can convert my pydantic enums to a ts type like:

class EnumExample(str, enum.Enum):
    THING = 'THING'
    COSA = 'COSA'

class ModelExample(BaseModel):
    my_enum: EnumExample

Where the converted enum type would look like:

export type EnumExample = "THING" | "COSA";

And EnumExample will be properly converted. However, if I have a solo enum with no corresponding model, that will not be converted.

Am I missing a step to get solo enums converted, or is this not yet a feature?

Thanks so much!

phillipdupuis commented 2 years ago

Hi, thank you and I'm glad you like it! :)

So you're not missing anything -- pydantic-to-typescript only finds and converts subclasses of pydantic.BaseModel. It's intended for applications which use pydantic models for exchanging data with the outside world.

I think that extending it to convert non-pydantic classes (like stand-alone enums) would make the purpose of this package a bit less clear... probably result in a lot of bug reports asking "why is my .ts file filled with all these random types?" 😬

I can help you throw together a little script for converting standalone enums, if it helps! But I think that it is probably not something that will be an official feature

robbieaverill commented 2 years ago

Hi @phillipdupuis, if you have an example script handy for converting enums that would be useful.

I understand not bloating the scope of your project. If we needed enums to be converted as well as Pydantic models, what would you recommend? I would prefer to avoid forking this package if possible - are there extensibility options we could explore?