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

fix: indicate that required properties are not null #50

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ColinEberhardt
Copy link
Collaborator

The code which is generated results in the following errors:

error TS2564: Property 'name' has no initializer and is not definitely assigned in the constructor.

Because the model objects are deserialised from JSON, it is not possible to create TS classes that demonstrate to the compiler that these properties are always set.

This change uses the null assertion operator to indicate that this value is not null:

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#non-null-assertion-operator

@OiNutter
Copy link

OiNutter commented Jun 1, 2023

I'm wondering if creating a constructor method would be better here, thinking from the perspective of extending classes. Honestly not sure if that's a likely use case or not, might just be my preference seeping through.

@ColinEberhardt
Copy link
Collaborator Author

Thanks for the suggestion @OiNutter - a potential alternative. Although using a constructor method would make the deserialization process more challenging. Currently an empty constructor is used, with properties set after construction:

https://github.com/ScottLogic/openapi-forge-typescript/blob/main/template/serializer.ts.handlebars#L18

I might experiment a bit with the constructor function approach.

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 this pull request may close these issues.

2 participants