Overview

About TypeGen

TypeGen is a command line tool that generates TypeScript sources from C# in a single-class-per-file manner. Its primary use case is keeping TypeScript model classes/interfaces in sync with C# models, and for this reason not all C# features are translated to TypeScript (e.g. methods or events will be ignored when generating TypeScript sources).

Requirements

For .NET Standard compatibility, see compatibility table.

Versions >= 2.0.0

  • CLI: .NET Core 2.1
  • TypeGen.Core: .NET Standard versions: 1.3 and 2.0

Versions 1.5.7 - 1.6.7

  • CLI: .NET Core 2.0
  • TypeGen.Core: .NET Standard versions: 1.3 and 2.0

Versions 1.5.0 - 1.5.6

  • CLI: .NET Framework 4.6
  • TypeGen.Core: .NET Standard 1.3

Versions <= 1.4.x

.NET Framework 4.0

Features

Main TypeGen’s features include:

  • generating TypeScript classes, interfaces and enums - single class per file
  • generating barrel (index) files
  • support for collection (or nested collection) property types
  • generic types generation
  • support for inheritance
  • customizable convertion between C#/TypeScript names (naming conventions)

Complete list of features added in each release is specified in the changelog.

How it works

TypeGen can be used in one of 2 ways:

  1. By specifying attributes on C# types you wish to generate. In this case, the big picture is illustrated on the diagram below:
_images/big-picture-attributes.png

In short, TypeGen gets selected C# types from one or more assemblies and generates corresponding TypeScript files for them in the file system.

Since TypeGen takes a .NET assembly, in theory it is also possible to provide an assembly written in a different language than C#. However, TypeGen is only tested with C# code.

  1. By creating one or more generation specs (a generation spec is a C# class which specifies which types to generate).
_images/big-picture-genspec.png

In general, attributes offer quicker, but more functionally-restricted way of selecting types to generate, whereas generation specs require more initial work to do, but offer richer functionality.

Getting started

After adding the TypeGen NuGet package to your project, TypeGen will be available in the Package Manager Console.

To use TypeGen as a .NET CLI tool (global), you can install it from this package.

Quick example

Let’s say you have a ProductDto class that you want to export to TypeScript.

  1. If you’re using attributes, just annotate your class with an appropriate attribute:
[ExportTsClass]
public class ProductDto
{
    public decimal Price { get; set; }
    public string[] Tags { get; set; }
}
  1. If you’re using a generation spec, first create your generation spec somewhere in your project:
public class MyGenerationSpec : GenerationSpec
{
    public MyGenerationSpec()
    {
        AddClass<ProductDto>();
    }
}

…and then create a file named tgconfig.json directly in your project folder and place the following content in this file:

{
    "generationSpecs": ["MyGenerationSpec"]
}

After finishing instructions described in either 1. or 2., build your project and type TypeGen generate or TypeGen -p "MyProjectName" generate (depending on the current working directory of the PM Console) into the Package Manager Console (you might have to restart Visual Studio). Instead of using the Package Manager Console, you can also use TypeGen as a .NET CLI tool by typing dotnet typegen generate in your OS shell.

Performing the steps described above will generate a single TypeScript file (named product-dto.ts) in your project directory. The file will look like this:

export class ProductDto {
    price: number;
    tags: string[];
}

To find out more about customization of various generation options, please visit the CLI section. You can also find out more about attributes or generation specs in their dedicated sections.