Skip to content

TypeScriptGenerator

Rino Reji Cheriyan edited this page May 12, 2022 · 41 revisions

The TypeScriptGenerator class generates TypeScript interfaces from a given JSON Schema:

var schema = JsonSchema.FromType<Person>();
var generator = new TypeScriptGenerator(schema);
var code = generator.GenerateFile();

Extended classes and extension code

Generated TypeScript classes can be extended with additional code (same functionality as partial classes in C#). To do so, specify all classes to extend in the ExtendedClasses setting. Now the extended classes can be implemented in an additional file which will be parsed and the copied into the generated code. This file or code can be specified using the ExtensionCode setting.

The sample extended classes ExtendedClasses: Person,Car

The sample ExtensionCode for the extended classes:

import generated = require("serviceClient");
// import generated from './serviceClient'; // also allowed
// import * as generated from './serviceClient'; // also allowed

class Person extends generated.Person {
    get name() {
        return this.firstName + " " + this.lastName;
    }
}

class Car extends generated.Car {
        
}

Important: Classes which are defined as ExtendedClasses are parsed and the class body is copied into the generated class. The class signature (i.e. class Person extends generated.Person) is ignored, only the body is copied. The rest of the code gets appended at the end of the file. Imports (except the generated import and imports which end with // ignore) and references (i.e. /// <reference path="..." />) are inserted at the beginning of the generated code.

An import can be filtered out in the generated code with the // ignore comment:

import { RequestOptionsArgs } from '@angular/http'; // ignore

See also

Datetime handling (e.g. MomentJS)