-
Notifications
You must be signed in to change notification settings - Fork 54
Add --no-optional-props #29
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
Conversation
Will this CLI option supress genuine optional fields as well? For example:
Results in this interface:
which behaves perfectly. Would applying the above CLI argument mean that this interface would become?
if so, would it be possible to be more specific about which kind of overrides to apply. I would suggest this rule:
I realise this level of expressiveness may not be exposed through Pydantic - but putting my thoughts out there. Thanks. |
Actually this:
is not "the" interface for this:
The reason is, it depends on whether you are specifying
If you want the latter behavior then the The type you gave accurately describes the dicts that can be passed into the class ctor. The type that
which is actually a different type in TS. Admittedly this is not what my change The real issue being addressed here is not actually
then pydantic-to-typescript produces
which is indeed the type of dict which may be expanded into
which is the type returned by For example:
|
Actually, the interface would need to look like
But nullable fields are not supported by json-schema-to-typescript (there is a PR thought). So you may consider working with "undefined only" in your API (by using To clarify, this PR would define the optional |
Do you have a version of this you have deployed as a package? |
No |
By default, properties of models which have default values will be marked as optional in the generated TS interfaces. This makes sense if you think of the TS interfaces as defining what may legally be passed into the Python model's constructor. However, if you want the TS interfaces to represent the type of the output produced by calling
.dict()
on an instance of the model, you don't want the TS interface types' properties marked optional, since they will always be there.This is particularly annoying when working with discriminated unions.
I don't think there is a general solution to this problem because the type accepted by the model constructor is actually different than the type emitted by
.dict()
, and you might want either one depending on your use case. So we add a command-line option making this behavior toggleable.This PR should provide a clean work-around for #28.