TypeGen

repository·master·Indexed 42 days ago

https://github.com/jburzynski/typegen

A tool that generates TypeScript definitions, including classes, interfaces, and enums, from C# classes to ensure type safety between .NET backends and TypeScript frontends. It supports inheritance, generic types, and customizable naming conventions, and can be used via the TypeGen NuGet package or the dotnet-typegen CLI tool.

Tokens
717
Snippets
4
Records
5
Agent score
4%

What's inside TypeGen

  1. Overview of TypeGen features

    master

    TypeGen is a single-class-per-file C# to TypeScript generator. Key features include:

    • Generating TypeScript classes, interfaces, and enums (one file per class).
    • Generating barrel (index) files.
    • Support for collections and generic types.
    • Support for inheritance.
    • Customizable translation of naming conventions.
  2. Select C# types to export to TypeScript

    master

    TypeGen allows you to specify which C# classes should be converted to TypeScript using two different methods:

    Method 1: Using Attributes

    Decorate your C# classes with the [ExportTsClass] attribute.

    Method 2: Using a Generation Spec

    Create a class that inherits from GenerationSpec and use the AddClass<T>() method to register types. This is useful if you want to manage exports in a centralized file rather than decorating every class.

    // Method 1: Using attributes
    [ExportTsClass]
    public class ProductDto
    {
        public decimal Price { get; set; }
        public string[] Tags { get; set; }
    }
    
    // Method 2: Using a specification file
    public class MyGenerationSpec : GenerationSpec
    {
        public MyGenerationSpec()
        {
            AddClass<ProductDto>();
        }
    }
  3. Install TypeGen

    master

    You can obtain TypeGen via NuGet in two ways depending on your needs:

    1. As a library: Add the TypeGen NuGet package to your project to use attributes for type selection.
    2. As a .NET CLI tool: Install the dotnet-typegen NuGet package to run generation commands from the system console or Package Manager Console.
    # Use the TypeGen NuGet package for library integration
    # Use the dotnet-typegen NuGet package for CLI tool usage
  4. Generate TypeScript files

    master

    After configuring your types and tgconfig.json, you can trigger the generation process using the following commands:

    Using the .NET Tool (System Console)

    Run this command from your terminal: dotnet typegen generate

    Using the Package Manager Console (Visual Studio)

    Run one of these commands (you may need to restart Visual Studio after installation):

    • TypeGen generate
    • TypeGen -p "MyProjectName" generate (if not in the project directory)
    dotnet typegen generate
  5. Configure TypeGen with a generation spec

    master

    If you choose to use a GenerationSpec class instead of attributes, you must create a tgconfig.json file in your project folder to tell TypeGen which spec to use. Include the name of your spec class in the generationSpecs array.

    {
      "generationSpecs": ["MyGenerationSpec"]
    }