SushiScript.TypeScript

Library for converting .NET models to different script languages.


Keywords
C#, JavaScript, TypeScript, Generation, Compilation, Compiler, Generator, Script, Model, Intellisense, Visual, Studio, .NET, Customizable, Custom, Framework, Unit-Test, EcmaScript, Ecma, Java, c-sharp, c-sharp-library, code-generation, code-generator, ecmascript6, extension-methods, validation, validator
License
MIT
Install
Install-Package SushiScript.TypeScript -Version 1.1.1

Documentation

-# Sushi Library for converting .NET classes to script language classes.

Currently supports

Author

Jeroen Vorsselman @ 2023

GitHub

NuGet


Features

  • Converts .NET classes to script languages (typescript / ECMAScript)
  • Compiled using .NET Standard 2.0
  • Supports native types, type inheritance, generics and enum types
  • Adds documentation using the generated MS build XML file
  • 95% Code coverage

About

string xmlDocPath =  Path.Combine(Environment.CurrentDirectory, "Sushi.tests.xml");

// Specify the types to convert using a Type[] or Assembly.ExportedTypes.
Assembly assembly = typeof(PersonViewModel).Assembly;
SushiConverter converter = new SushiConverter(assembly).UseDocumentation(xmlDocPath);

// Specify the script language and convert by invoking ToString().
ConverterOptions options = new ConverterOptions(excludeComments: true);
string result = converter.TypeScript(options).ToString();

Create a new SushiConverter instance with the given Assembly or Type[] that contain the types you want to convert.
These classes must be decorated with the ConvertToScriptAttribute or inherit the IScriptModel interface. Classes can be excluded using the IgnoreForScriptAttribute. The converter contains a collection of type- and enum-descriptors. These are used to generate script models.

Helpers

You can check if a type exists using: IsSushiType(IConvertModels converter, Type type, out Type resolvedType) : boolean
You can convert types using: TypeScriptConverter.ResolveScriptType(Type type, string prefix = "") : string
You can get the script default value using: TypeScriptConverter.ResolveDefaultValue(IPropertyDescriptor prop) : string

Typescript result

/**
 * Simple model to verify complex types.
 * Sushi.Tests.Models.TypeModel
 * @extends ViewModel
 */
export class TypeModel extends ViewModel {

    /**
     * A nullable boolean.
     * @type (boolean | null)
     */
    nullableBool: boolean | null = null;

    /**
     * A nullable string, defaults to null.
     * @type (string)
     */
    nullableString: string = "";

    /**
     * A DateTime instance.
     * @type (Date | string | null)
     */
    date!: Date | string | null;
    student: StudentViewModel = new StudentViewModel();
    students: Array<StudentViewModel> = [];
    studentPerClass: { [key: string]: Array<StudentViewModel> } = {};

    /**
     * A readonly string.
     * @type (string)
     */
    static readonly readonlyString: string = "readonly";

    constructor(value: Partial<TypeModel> = {}) {
        super(value);

        if (value.nullableBool !== undefined) this.nullableBool = value.nullableBool;
        if (value.nullableString !== undefined) this.nullableString = value.nullableString;
        if (value.guid !== undefined) this.guid = value.guid;
        if (value.date !== undefined) this.date = value.date;
        if (value.student !== undefined) this.student = value.student;
        if (value.students !== undefined) this.students = value.students;
        if (value.studentPerClass !== undefined) this.studentPerClass = value.studentPerClass;
        if (value.createdOn !== undefined) this.createdOn = value.createdOn;
    }
}