-
-
Notifications
You must be signed in to change notification settings - Fork 535
TypeScriptGenerator
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();
Class: TypeScriptGeneratorSettings inherits from CodeGeneratorSettingsBase
-
TypeStyle:
-
Interface
(default): Generates interfaces -
Class
: Generates classes (lower camel cased fields, correct date handling, default values, extendable) -
KoObservableClass
: Generates classes with KnockoutJS observable fields (same advantages as with theClass
type style)
-
- GenerateReadOnlyKeywords (default: true)
-
ClassTypes (list of class/type names): The type names which always generate plain TypeScript classes (i.e. force a TypeStyle of
Class
) - ExtendedClasses: Defines classes which are extendend in the generated TypeScript classes (same functionality as partial classes in C#)
-
ExtensionCode: Specifies additional code which is appended (used in conjunction with
ExtendedClasses
). Can be the TypeScript code or a file path to a TypeScript file, when used as parameter in the NSwag command line
Generated TypeScript classes can be extended with additional code via inheritance (same functionality as partial classes in C#). To do so, specify all classes to extend in the ExtendedClasses
configuration. All these classes are generated with a Base
postfix. Now the extended classes can be implemented in an additional file which will be appended to the generated code. This file or code can be specified using the ExtendedCode
configuration.
The sample extended classes ExtendedClasses
: Person,Car
The sample ExtensionCode
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 {
}
Important: Classes which are defined as ExtendedClasses
are inserted after their generated base class and the class is automatically decorated with the export
keyword. The rest of the code gets appended at the end of the file. Imports (except the generated
import which is removed) and references (i.e. /// <reference path="..." />
) are inserted at the beginning of the generated code.