### Generate TypeScript Files using .NET CLI Source: https://context7.com/jburzynski/typegen/llms.txt Run TypeGen from the command line using the dotnet CLI tool. Install the tool globally, then use 'dotnet typegen generate' with optional project path and verbose flags. ```bash # Using .NET CLI tool (dotnet-typegen) # Install globally dotnet tool install -g dotnet-typegen # Generate TypeScript files using tgconfig.json in current directory dotnet typegen generate # Generate with specific project path dotnet typegen generate -p ./MyProject # Generate with verbose output dotnet typegen generate -v ``` -------------------------------- ### Configure Class Generation Fluently with ClassSpecBuilder Source: https://context7.com/jburzynski/typegen/llms.txt Utilize the fluent builder pattern within a GenerationSpec to configure class generation at a member level. This allows for fine-grained control over individual properties, such as ignoring them, setting custom types, default values, or marking them as readonly. ```csharp using TypeGen.Core.SpecGeneration; public class AdvancedGenerationSpec : GenerationSpec { public AdvancedGenerationSpec() { AddClass() .CustomHeader("// Custom header comment") .DefaultExport() .Member(nameof(UserDto.Password)).Ignore() .Member(nameof(UserDto.CreatedAt)).Type("moment.Moment", "./moment") .Member(nameof(UserDto.Role)).DefaultValue(""user""); AddClass() .Member(nameof(ConfigDto.Settings)).Type(TsType.Any) .Member(nameof(ConfigDto.IsEnabled)).DefaultValue("false") .Member(nameof(ConfigDto.Count)).Static() .Member(nameof(ConfigDto.Version)).Readonly(); } } public class UserDto { public int Id { get; set; } public string Email { get; set; } public string Password { get; set; } public DateTime CreatedAt { get; set; } public string Role { get; set; } } public class ConfigDto { public object Settings { get; set; } public bool IsEnabled { get; set; } public static int Count { get; set; } public string Version { get; set; } } ``` -------------------------------- ### Generate TypeScript from C# Source: https://github.com/jburzynski/typegen/blob/master/README.md Commands to trigger the TypeGen generation process. Use 'TypeGen generate' in the Package Manager Console or 'dotnet typegen generate' in the system console. ```bash TypeGen generate ``` ```bash TypeGen -p "MyProjectName" generate ``` ```bash dotnet typegen generate ``` -------------------------------- ### Build TypeGen Project Source: https://github.com/jburzynski/typegen/blob/master/README.md Commands to update the version and publish the TypeGen NuGet packages. ```powershell .\update-version.ps1 3.0.0 .\publish.ps1 ``` -------------------------------- ### Configure TypeGen with JSON Source: https://github.com/jburzynski/typegen/blob/master/README.md Create a tgconfig.json file in your project root to specify the generation specs to be used. This file is essential when using the generation spec approach. ```json { "generationSpecs": ["MyGenerationSpec"] } ``` -------------------------------- ### Configure TypeGen CLI with tgconfig.json Source: https://context7.com/jburzynski/typegen/llms.txt Use tgconfig.json to set project-wide TypeScript generation options for the TypeGen CLI. Configure assemblies, output paths, file extensions, naming conventions, and type mappings. ```json { "assemblies": ["MyProject.dll"], "generationSpecs": ["MyGenerationSpec"], "outputPath": "./ClientApp/src/api/models", "clearOutputDirectory": false, "createIndexFile": true, "typeScriptFileExtension": "ts", "tabLength": 2, "useTabCharacter": false, "singleQuotes": true, "explicitPublicAccessor": false, "fileNameConverters": ["PascalCaseToKebabCaseConverter"], "typeNameConverters": [], "propertyNameConverters": ["PascalCaseToCamelCaseConverter"], "enumValueNameConverters": [], "enumStringInitializers": false, "csNullableTranslation": "null", "csAllowNullsForAllTypes": false, "useDefaultExport": false, "exportTypesAsInterfacesByDefault": false, "useImportType": false, "customTypeMappings": { "System.Guid": "string", "System.DateTime": "string", "System.DateTimeOffset": "string" }, "defaultValuesForTypes": { "number": "0", "boolean": "false" }, "typeBlacklist": [ "System.Object" ], "fileHeading": "/* Auto-generated - do not edit */" } ``` -------------------------------- ### Generate TypeScript Files using Package Manager Console Source: https://context7.com/jburzynski/typegen/llms.txt Execute TypeGen generation commands within Visual Studio's Package Manager Console. Use 'TypeGen generate' or 'TypeGen -p "ProjectName" generate' for specific projects. ```powershell # Using Package Manager Console (TypeGen package) # In Visual Studio Package Manager Console TypeGen generate # Generate for specific project TypeGen -p "MyProject" generate ``` -------------------------------- ### Configure GeneratorOptions for TypeScript Generation Source: https://context7.com/jburzynski/typegen/llms.txt Customize TypeScript generation behavior using GeneratorOptions. This includes output configuration, formatting, naming conventions, nullability handling, custom type mappings, and export options. ```csharp using TypeGen.Core.Generator; using TypeGen.Core.Converters; var options = new GeneratorOptions { // Output configuration BaseOutputDirectory = "./generated", TypeScriptFileExtension = "ts", CreateIndexFile = true, // Formatting TabLength = 4, UseTabCharacter = false, SingleQuotes = false, ExplicitPublicAccessor = false, // Naming converters FileNameConverters = new TypeNameConverterCollection( new PascalCaseToKebabCaseConverter() ), PropertyNameConverters = new MemberNameConverterCollection( new PascalCaseToCamelCaseConverter() ), TypeNameConverters = new TypeNameConverterCollection(), EnumValueNameConverters = new MemberNameConverterCollection(), // Nullability handling CsNullableTranslation = StrictNullTypeUnionFlags.Null | StrictNullTypeUnionFlags.Undefined, CsAllowNullsForAllTypes = false, // Custom type mappings CustomTypeMappings = new Dictionary { { "System.Guid", "string" }, { "System.DateTime", "string" }, { "System.TimeSpan", "string" } }, // Default values for types DefaultValuesForTypes = new Dictionary { { "number", "0" }, { "string", "\"\"" }, { "boolean", "false" } }, // Type unions TypeUnionsForTypes = new Dictionary> { { "string", new[] { "null" } } }, // Export options UseDefaultExport = false, ExportTypesAsInterfacesByDefault = false, UseImportType = false, // File heading FileHeading = "// Auto-generated by TypeGen" }; var generator = new Generator(options); ``` -------------------------------- ### Define C# Generation Specification Source: https://github.com/jburzynski/typegen/blob/master/README.md Create a class that inherits from GenerationSpec and add the C# classes to be exported. This allows for a more structured approach to defining exportable types. ```csharp // or using a specification file (a "generation spec") created anywhere in your project public class MyGenerationSpec : GenerationSpec { public MyGenerationSpec() { AddClass(); } } ``` -------------------------------- ### Define Types for Generation Programmatically with GenerationSpec Source: https://context7.com/jburzynski/typegen/llms.txt Create a custom class inheriting from GenerationSpec to define which types to generate and configure their output programmatically using a fluent API. This approach avoids using attributes directly on C# classes. ```csharp using TypeGen.Core.SpecGeneration; public class MyGenerationSpec : GenerationSpec { public MyGenerationSpec() { // Add a class with default settings AddClass(); // Add a class with custom output directory AddClass("generated/models"); // Add an interface AddInterface(); // Add an enum AddEnum(); // Add enum as const enum AddEnum(isConst: true); // Add barrel file for a directory AddBarrel("generated/models"); } } // Usage with tgconfig.json: // { // "generationSpecs": ["MyGenerationSpec"], // "outputPath": "./ClientApp/src/api" // } ``` -------------------------------- ### Export C# Class as TypeScript Class Source: https://context7.com/jburzynski/typegen/llms.txt Use the ExportTsClass attribute to mark a C# class for export as a TypeScript class. Specify the output directory using OutputDir. ```csharp using TypeGen.Core.TypeAnnotations; [ExportTsClass(OutputDir = "models")] public class ProductDto { public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } public string[] Tags { get; set; } } ``` -------------------------------- ### Programmatic TypeScript Generation with Generator Source: https://context7.com/jburzynski/typegen/llms.txt Use the Generator class for programmatic control over TypeScript file generation from C# types. Supports basic usage, custom options, generation from assemblies, and custom file handling. ```csharp using TypeGen.Core.Generator; using TypeGen.Core.SpecGeneration; // Basic usage with default options var generator = new Generator(); var generatedFiles = generator.Generate(typeof(ProductDto)); // With custom options var options = new GeneratorOptions { BaseOutputDirectory = "./ClientApp/src/models", SingleQuotes = true, TabLength = 2, UseTabCharacter = false, CreateIndexFile = true, CsNullableTranslation = StrictNullTypeUnionFlags.Null, ExportTypesAsInterfacesByDefault = true }; var generator = new Generator(options); var files = generator.Generate(typeof(ProductDto), typeof(OrderDto)); // Generate from assembly var assembly = typeof(ProductDto).Assembly; var files = generator.Generate(assembly); // Generate from GenerationSpec var spec = new MyGenerationSpec(); var files = generator.Generate(spec); // Async generation var files = await generator.GenerateAsync(spec); // Custom file handling generator.UnsubscribeDefaultFileContentGeneratedHandler(); generator.FileContentGenerated += (sender, args) => { Console.WriteLine($"Generated: {args.FilePath}"); // Custom file saving logic File.WriteAllText(args.FilePath, args.FileContent); }; generator.Generate(spec); ``` -------------------------------- ### Generated TypeScript Class Source: https://github.com/jburzynski/typegen/blob/master/README.md The resulting TypeScript class generated from the C# ProductDto. Note the conversion of C# types to their TypeScript equivalents. ```typescript export class ProductDto { price: number; tags: string[]; } ``` -------------------------------- ### Configure Interface Generation with InterfaceSpecBuilder Source: https://context7.com/jburzynski/typegen/llms.txt Use InterfaceSpecBuilder to fluently configure TypeScript interface generation, including optional properties and type overrides. This is useful for defining the structure of interfaces to be generated. ```csharp using TypeGen.Core.SpecGeneration; public class InterfaceGenerationSpec : GenerationSpec { public InterfaceGenerationSpec() { AddInterface() .Member(nameof(ApiResponse.Data)).Type(TsType.Any) .Member(nameof(ApiResponse.Error)).Optional() .Member(nameof(ApiResponse.Metadata)).Optional().Null(); AddInterface("api/params") .CustomBase("BaseParams", "./base-params") .Member(nameof(QueryParams.Limit)).Optional().DefaultValue("50") .Member(nameof(QueryParams.Offset)).Optional(); } } public class ApiResponse { public object Data { get; set; } public string Error { get; set; } public Dictionary Metadata { get; set; } } public class QueryParams { public string Filter { get; set; } public int Limit { get; set; } public int Offset { get; set; } } ``` -------------------------------- ### Export C# Class as TypeScript Interface Source: https://context7.com/jburzynski/typegen/llms.txt Employ the ExportTsInterface attribute to export a C# class as a TypeScript interface, useful for type definitions without runtime overhead. The OutputDir parameter specifies the generation location. ```csharp using TypeGen.Core.TypeAnnotations; [ExportTsInterface(OutputDir = "interfaces")] public class UserDto { public Guid Id { get; set; } public string Email { get; set; } public DateTime CreatedAt { get; set; } public bool IsActive { get; set; } } ``` -------------------------------- ### Export C# Class with Attribute Source: https://github.com/jburzynski/typegen/blob/master/README.md Use the ExportTsClass attribute to mark C# classes for TypeScript export. Ensure the class properties are public. ```csharp // using attributes [ExportTsClass] public class ProductDto { public decimal Price { get; set; } public string[] Tags { get; set; } } ``` -------------------------------- ### Customize Base Class/Interface with TsCustomBase Source: https://context7.com/jburzynski/typegen/llms.txt Use the TsCustomBase attribute to specify a custom base class or interface for the generated TypeScript type. You can also configure imports for these custom types. ```csharp using TypeGen.Core.TypeAnnotations; [ExportTsClass] [TsCustomBase("BaseEntity", "./base-entity")] public class ArticleDto { public int Id { get; set; } public string Title { get; set; } public string Content { get; set; } } ``` ```csharp using TypeGen.Core.TypeAnnotations; [ExportTsClass] [TsCustomBase("BaseModel", "./base", null, false, "ISerializable", "./serializable", null, false)] public class DocumentDto { public Guid Id { get; set; } public string Name { get; set; } } ``` -------------------------------- ### Mark Property as Optional in TypeScript Source: https://context7.com/jburzynski/typegen/llms.txt Use the TsOptional attribute to mark a C# property as optional in the generated TypeScript interface. This adds a '?' suffix to the property name. ```csharp using TypeGen.Core.TypeAnnotations; [ExportTsInterface] public class SearchRequest { public string Query { get; set; } [TsOptional] public int? Page { get; set; } [TsOptional] public int? PageSize { get; set; } [TsOptional] public string[] Filters { get; set; } } ``` -------------------------------- ### Set Default Value for Property with TsDefaultValue Source: https://context7.com/jburzynski/typegen/llms.txt Use the TsDefaultValue attribute to specify a default value for a TypeScript class property. Ensure the value is correctly formatted as a string literal for the target type. ```csharp using TypeGen.Core.TypeAnnotations; [ExportTsClass] public class PaginationSettings { [TsDefaultValue("1")] public int Page { get; set; } [TsDefaultValue("10")] public int PageSize { get; set; } [TsDefaultValue(""id"")] public string SortBy { get; set; } [TsDefaultValue("true")] public bool Ascending { get; set; } } ``` -------------------------------- ### Export C# Enum as TypeScript Enum Source: https://context7.com/jburzynski/typegen/llms.txt Utilize the ExportTsEnum attribute to convert C# enums to TypeScript enums. The IsConst parameter controls whether to generate a const enum, and AsUnionType can generate a union type instead. ```csharp using TypeGen.Core.TypeAnnotations; [ExportTsEnum(OutputDir = "enums", IsConst = false)] public enum OrderStatus { Pending = 0, Processing = 1, Shipped = 2, Delivered = 3, Cancelled = 4 } ``` ```csharp // With AsUnionType = true: [ExportTsEnum(OutputDir = "enums", AsUnionType = true)] public enum Priority { Low, Medium, High } ``` -------------------------------- ### Exclude Property from TypeScript Generation with TsIgnore Source: https://context7.com/jburzynski/typegen/llms.txt Apply the TsIgnore attribute to properties or fields that should not be included in the generated TypeScript code. This is useful for internal or sensitive data. ```csharp using TypeGen.Core.TypeAnnotations; [ExportTsClass] public class CustomerDto { public int Id { get; set; } public string Name { get; set; } public string Email { get; set; } [TsIgnore] public string PasswordHash { get; set; } [TsIgnore] internal string InternalNote { get; set; } } ``` -------------------------------- ### Override TypeScript Type for Property Source: https://context7.com/jburzynski/typegen/llms.txt The TsType attribute allows specifying a custom TypeScript type for a C# property or field, including an optional import path for external types. TsType.Any can be used for generic object types. ```csharp using TypeGen.Core.TypeAnnotations; [ExportTsClass] public class EventDto { public int Id { get; set; } [TsType("moment.Moment", "./moment")] public DateTime EventDate { get; set; } [TsType(TsType.Any)] public object Metadata { get; set; } [TsType("CustomType", "./custom-type", "OriginalCustomType")] public dynamic CustomData { get; set; } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.