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

Add option to represent non-required properties as required fields in TS with | undefined #1606

Open
1 task
Gelio opened this issue Apr 4, 2024 · 0 comments
Labels
enhancement New feature or request help wanted Extra attention is needed openapi-ts Relevant to the openapi-typescript library

Comments

@Gelio
Copy link

Gelio commented Apr 4, 2024

Description

Proposal

I suggest adding an option that would determine whether non-required object properties get turned into:

  someProperty?: string;

or

  someProperty: string | undefined;

The latter seems safer to work with as a producer of objects that have to match the generated TypeScript types. When adding a new non-required property to an object, I want TypeScript to point out to me all the places in which I should consider adding/skipping that property.

Right now after adding a new non-required property, TypeScript will happily accept existing code, since those existing objects are still valid, because the property is optional. If the property was declared as required, but undefinable (| undefined), I would have to manually go through all the places where that property should appear and either set it to a valid value, or set it to someProperty: undefined (if it should be omitted).

I understand this is a breaking change, which is why I suggest making it a CLI option, rather than changing the default behavior.

For reference, ts-proto has a useOptionals option that decides whether optional properties are generated using ? or | undefined. They recommend | undefined as the default behavior (the behavior I want also from openapi-typescript).

Checklist

@Gelio Gelio added enhancement New feature or request help wanted Extra attention is needed openapi-ts Relevant to the openapi-typescript library labels Apr 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed openapi-ts Relevant to the openapi-typescript library
Projects
None yet
Development

No branches or pull requests

1 participant