Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support inheritance when generating TS interfaces #48

Open
guaycuru opened this issue Oct 10, 2024 · 2 comments
Open

Support inheritance when generating TS interfaces #48

guaycuru opened this issue Oct 10, 2024 · 2 comments

Comments

@guaycuru
Copy link

guaycuru commented Oct 10, 2024

Currently if I have

class ChildClass(ParentClass):
  something: str

class ParentClass(BaseModel):
  anything: str

The generated output is:

export interface ChildClass {
  something: string;
  anything: string;
}

export interface ParentClass {
  anything: string;
}

When a better output would be:

export interface ChildClass extends ParentClass {
  something: string;
}

export interface ParentClass {
  anything: string;
}

It seems https://github.com/bcherny/json-schema-to-typescript supports this. See this example.

@phillipdupuis
Copy link
Owner

This is a good question/thought. But I don't think there is a solution, or at least not a simple one :(

This open issue (pydantic/pydantic#8161) seems to be about the same problem, albeit with allOf rather than extends. And it does not sound like it's going to be implemented by pydantic any time soon.

Will need to think on this

Maybe if you had a custom schema generator function with a check like

if a parent exists:
    add the parent ref via "extends" or "allOf"
    then iterate through "properties" for both parent and child, deleting from "child" if the definition is unchanged

@guaycuru
Copy link
Author

Yeah, I have a somewhat working proptotype, but I keep running into edge cases.
It's on the back burner for now as work has been crazy lately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants