Skip to content

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