Skip to content

Latest commit

 

History

History
69 lines (42 loc) · 3.47 KB

TypeScript.md

File metadata and controls

69 lines (42 loc) · 3.47 KB

TypeScript

There are special use-cases that each language supports; this document pertains to TypeScript models.

Generate an interface instead of classes

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.

Generate union types instead of enums

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.

Generate un/marshal functions for classes

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.

Generate example data function

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.

Rendering complete models to a specific module system

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.

Rendering comments from description and example fields

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.