Skip to content

Field with default value becomes optional on the TypeScript interface #1

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

Closed
alythobani opened this issue Apr 11, 2025 · 0 comments · Fixed by #2
Closed

Field with default value becomes optional on the TypeScript interface #1

alythobani opened this issue Apr 11, 2025 · 0 comments · Fixed by #2

Comments

@alythobani
Copy link
Owner

Copied over from phillipdupuis#28:

Example:

class MyModel(BaseModel):
  my_bool_property: bool = False

Convert to TypeScript and see output:

interface MyModel {
  my_bool_property?: bool;
}

I believe that the interface shouldn't define the property as optional, since Pydantic will ensure it is hydrated with the default value of false if it's not provided when creating models, e.g.:

newModel = MyModel()
assert newModel.my_bool_property is False  # assertion passes

Expected TypeScript interface:

interface MyModel {
  my_bool_property: bool;
}
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

Successfully merging a pull request may close this issue.

1 participant