Disclamer, these docs are made by chatGpt
SharpTypes is a C# library designed to automatically generate TypeScript types from C# DTOs using reflection. It helps maintain consistency between your C# models and TypeScript types, making it especially useful for monorepo setups or projects where backend and frontend need to share data models.
- 📦 Generate TypeScript Types directly from your C# DTOs.
- 🔄 Reflection-Based scanning of assemblies to auto-detect eligible DTOs.
- 🔧 Highly Configurable:
- Specify output directories.
- Generate a barrel
index.ts
file for clean re-exports. - Optionally tag DTOs with custom output folders.
- 🔍 Supports Complex Types such as collections, nullable types, and custom attributes for fine-tuned control.
- 🏷 Barrel Export Support: Automatically generate an
index.ts
that re-exports your types in a clean, organized way. - 🔄 Monorepo Ready: Ideal for projects where you need to sync types across multiple repositories or packages.
You can add SharpTypes to your project by cloning the repo or including it as a dependency in your solution.
You can integrate SharpTypes into your ASP.NET Core project by registering the TypeScript generator services in Program.cs
.
using SharpTypes.Extensions;
var builder = WebApplication.CreateBuilder(args);
// Register and configure SharpTypes services
builder.Services.AddTypeScriptGenerator(options =>
{
options.DefaultOutputDirectory = @"C:\typescript\generated"; // Where the TypeScript files will be output
options.Assemblies = new[] { Assembly.GetExecutingAssembly().Location }; // Specify which assemblies to scan
options.GenerateIndexFile = true; // Enable/disable barrel export generation
});
var app = builder.Build();
// Use SharpTypes TypeScript generation on startup
app.UseTypeScriptGeneration();
app.Run();
Simply annotate your C# DTOs with [ExportTs]
to mark them for TypeScript generation.
namespace MyApp.DTO
{
[ExportTs] // Mark this DTO for TypeScript generation
public class ProductDto
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
}
You can also tag DTOs with custom folders using the [ExportToFolder]
attribute, which organizes generated files into specific directories.
namespace MyApp.DTO.FeatureA
{
[ExportTs]
[ExportToFolder("featureA")] // Custom folder for this DTO
public class OrderDto
{
public int OrderId { get; set; }
public DateTime Date { get; set; }
public decimal Total { get; set; }
}
}
SharpTypes can generate a barrel index.ts
file, which re-exports all your DTOs in a clean, organized way:
export { default as ProductDto } from './FeatureA/ProductDto';
export { default as OrderDto } from './FeatureA/OrderDto';
This makes it easy to import your types in your frontend applications:
Copy code
import { ProductDto, OrderDto } from './types';
Configuration Options You can customize the behavior of SharpTypes via the AddTypeScriptGenerator configuration options:
You can customize the behavior of SharpTypes via the AddTypeScriptGenerator
configuration options:
Option | Description |
---|---|
DefaultOutputDirectory |
The directory where TypeScript files will be generated (default: Directory.GetCurrentDirectory() ). |
Assemblies |
The assemblies to scan for DTOs. Pass in an array of assemblies. |
GenerateIndexFile |
Set to true to generate a barrel index.ts that re-exports all DTOs. |
SharpTypes supports:
- Nullable types (
int?
,bool?
, etc.) — automatically treated as optional in TypeScript. - Collections (
List<T>
,IEnumerable<T>
) — converted to TypeScript arrays (T[]
). - Inheritance — DTOs that inherit from other DTOs are properly reflected in the generated TypeScript.
Example Folder Structure After running SharpTypes, your project might look like this:
Copy code
/shared/types/
│
├── FeatureA/
│ ├── ProductDto.ts
│ ├── OrderDto.ts
│
└── index.ts // Automatically generated if enabled
We welcome contributions to improve SharpTypes! If you have any bug reports, feature requests, or suggestions, please open an issue or submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
SharpTypes helps you maintain consistency between your C# models and TypeScript types, making your development experience seamless and efficient across both backend and frontend applications.