There are special use-cases that each language supports; this document pertains to TypeScript models.
- Generate an interface instead of classes
- Generate union types instead of enums
- Generate un/marshal functions for classes
- Generate example data function
- Rendering complete models to a specific module system
- Rendering comments from description and example fields
Sometimes you don't care about classes, but rather have interfaces generated. This can be changed through the modelType configuration.
Check out this example out for a live demonstration.
Typescript offers union types which can simplify the use as no keywords are needed and the values can be set directly. This behavior can be changed through the modelType configuration. An example of the generated code can be seen below:
// enumType = 'enum'
export enum Event = {
PING: "ping",
PONG: "pong"
};
// enumType = 'union'
export type Event = "ping" | "pong";
Check out this example out for a live demonstration.
Sometimes you want to use the models for data transfers, and while most cases would work out of the box, custom serializer functionality is needed for the advanced cases. If you generated the data models based on a JSON Schema document and you want the serialized data to validate against the schema, this functionality is REQUIRED.
This can be done by including the preset TS_COMMON_PRESET
using the option marshalling
.
Check out this example out for a live demonstration.
You might stumble upon a user case (we had one in code generation) where you want a simple example instance of the generated data model.
This can be done by including the preset TS_COMMON_PRESET
using the option example
.
Check out this example out for a live demonstration.
In some cases you might need to render the complete models to a specific module system such as ESM and CJS.
You can choose between default exports and named exports when using either, with the exportType
option.
Check out this example for a live demonstration how to generate the complete TypeScript models to use ESM module system.
Check out this example for a live demonstration how to generate the complete TypeScript models to use CJS module system.
You can use the TS_DESCRIPTION_PRESET
to generate JSDoc style comments from description and example fields in your model.
See this example for how this can be used.