Skip to content

TypeScriptGenerator

Rico Suter edited this page May 17, 2016 · 41 revisions

Package: NJsonSchema.CodeGeneration

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

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

Settings

Class: TypeScriptGeneratorSettings

  • TypeStyle: Interface (default) | Class | KoObservableClass
  • GenerateReadOnlyKeywords (default: true)
  • ClassMappings: Defines class mappings to extend the generated TypeScript classes (same functionality as partial classes in C#)
  • AdditionalCode: Specifies additional code which is appended to the generated class (used in conjunction with ClassMappings)

Extended classes and extension code

Generated TypeScript classes can be extended with additional code (same functionality as partial classes in C#) via inheritance. To do so, specify all extended classes with the ExtendedClasses configuration. All these classes are generated with a Base postfix. Now the extended classes can be implemented in an additional file which must be appended to the generated code using the ExtendedCode configuration.

The sample extended classes: Person, Car

The sample ExtendedCode for the extended classes:

import generated = require("myclasses");

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

class Car extends generated.CarBase {
		
}

The generated import is automatically removed before the code is appended and all extended classes are automatically exported.

Clone this wiki locally