freshcrate
Skin:/
Home > Developer Tools > typesharp

typesharp

TypeSharp CLI - Automatically generate TypeScript from C# models. Keep your frontend and backend types in perfect sync! Supports nullable types, enums, inheritance, arrays, and custom naming conventio

Why this rank:Recent releaseHealthy release cadenceStrong adoption

Description

TypeSharp CLI - Automatically generate TypeScript from C# models. Keep your frontend and backend types in perfect sync! Supports nullable types, enums, inheritance, arrays, and custom naming conventions. Perfect for ASP.NET Core + Vue/Nuxt/React projects.

README

TypeSharp

License: MIT npm version npm downloads GitHub commits GitHub last commit GitHub stars Sponsor

Generate TypeScript types from C# models with ease! TypeSharp scans your ASP.NET Core projects and automatically generates TypeScript interfaces from your C# classes and records decorated with the [TypeSharp] attribute.

Project structure: docs/project-structure

Features

  • Automatic Type Generation – Convert C# models to TypeScript interfaces
  • Record Support – Positional records, record class, record struct, and body-only records
  • Custom Attribute Targeting – Use [TypeSharp] or any custom attribute
  • Nullable Support – string? → string | null
  • Collection Handling – Supports List<T>, IEnumerable<T>, arrays and generic collections
  • Dictionary Mapping – Dictionary<K, V> → Record<K, V>
  • Generic Types – Preserves generic type definitions like Response<T> → Response<T>
  • Inheritance – Preserves class and record inheritance using extends
  • Computed Properties – Expression-bodied and block getter properties are included
  • Naming Conventions – Convert property names (camel, pascal, snake, kebab)
  • Flexible Output – Single file or multiple files
  • Enum Support – Converts C# enums to TypeScript string enums
  • File Grouping – Preserves C# file organization (multiple classes per file stay together)
  • Auto Imports – Automatically generates import type statements between output files
  • Multi-Project – Scan multiple .csproj files in a single run
  • Obsolete Support – [Obsolete] / [Obsolete("...")] → /** @deprecated ... */ JSDoc

How TypeSharp Compares

This is not an OpenApi-based tool !

Feature TypeSharp NSwag openapi-typescript TypeGen
Direct C# parsing ✓ ✕ ✕ ✓
Attribute targeting ✓ ! ✕ !
Non-API models ✓ ✕ ✕ ✓
Generics preserved ✓ ! ! !
Record support ✓ ✕ ✕ ✕
File grouping ✓ ✕ ✕ ✕
Naming control ✓ ! ! ✕
API client generation ✕ ✓ ✕ ✕

Also see docs/why-typesharp

Installation

npm install -D @siyavuyachagi/typesharp

Quick Start

1. Install the NuGet attributes package

In your C# project:

dotnet add package TypeSharp.Attributes

2. Decorate your C# models or DTOs

Use [TypeSharp] on classes, records, or enums:

[TypeSharp]
public class User
{
  public int Id { get; set; }
  public string? Name { get; set; }
  public string Email { get; set; }
  public List<UserRole> Roles { get; set; }
  public List<string> Permissions { get; set; }
  public DateTime CreatedAt { get; set; }
}

[TypeSharp]
public record ProductSummary(int Id, string Name, decimal Price);

[TypeSharp]
public enum UserRole
{
  Admin,
  User,
  Guest
}

[TypeSharp]
public class ApiResponse<T>
{
  public bool Success { get; set; }
  public string? Message { get; set; }
  public T Data { get; set; }
  public List<string> Errors { get; set; }
}

3. Create a configuration file

In your frontend project run the following script

# Create TypeScript config
npx typesharp init

# ----- OR -------

# Create JSON config
npx typesharp init --format json

# Create TypeScript config (default)
npx typesharp init --format ts

# Create JavaScript config
npx typesharp init --format js

This creates typesharp.config.json:

{
  "source": [
    "C:/Users/User/Desktop/MyApp/Api/Api.csproj",
    "C:/Users/User/Desktop/MyApp/Domain/Domain.csproj"
  ],
  "outputPath": "./app/types",
  "targetAnnotation": "TypeSharp",
  "singleOutputFile": false,
  "namingConvention": "camel"
}

4. Generate TypeScript types

npx typesharp

# ----- OR -------

npx typesharp generate
# or with custom config
npx typesharp generate --config ./custom-config.ts

For more advanced usage docs/usage

Configuration

1. Configuration Options

Option Type Default Description
source string | string[] required Full path(s) to your C# .csproj file(s)
outputPath string required Where to generate TypeScript files
targetAnnotation string 'TypeSharp' C# attribute name to look for
singleOutputFile boolean false Generate one file or multiple files (see below)
namingConvention string | { dir: string, file: string } 'camel' Property/file/dir naming: kebab, camel, pascal, snake
fileSuffix string optional Suffix appended to generated file names: user-dto.ts

2. Naming Convention

namingConvention accepts either a simple string that applies to everything, or a config object for separate control over directories and files:

// Simple — applies to both dirs and files
{ "namingConvention": "kebab" }

// Advanced — separate control
{
  "namingConvention": {
    "dir": "kebab",
    "file": "camel"
  }
}

3. Output File Behavior

TypeSharp preserves your C# file organization. Here's how it works:

C# File Structure singleOutputFile: false singleOutputFile: true
One class per file
User.cs → 1 class
user.ts (1 interface) All classes in types.ts
Multiple classes per file
UserDtos.cs → 3 classes
user-dtos.ts (3 interfaces) All classes in types.ts
Mixed structure
Various C# files
Each C# file → 1 TS file
(preserves grouping)
All classes in types.ts

4. Configuration File Formats

TypeSharp supports multiple configuration formats:

JSON (typesharp.config.json): ⭐ recommended

{
  "source": ["C:/Users/User/Desktop/MyApp/Domain/Domain.csproj"],
  "outputPath": "./src/types"
}

JavaScript (typesharp.config.js):

const config = {
  source: ["C:/Users/User/Desktop/MyApp/Domain/Domain.csproj"],
  outputPath: "./src/types",
};

export default config;

TypeScript (typesharp.config.ts): âš ī¸ requires special setup

TypeScript config files require one of the following approaches:

  1. Convert to JavaScript first (recommended):

    tsc typesharp.config.ts --module nodenext
  2. Run with tsx loader:

    node --loader tsx/cjs ./node_modules/@siyavuyachagi/typesharp/bin/typesharp.js
  3. Or use a .js config file instead (simplest)

If you prefer TypeScript configs, JSON format is the easiest alternative that provides type safety through JSDoc in most editors.

Usage in package.json

Add TypeSharp to your build scripts:

{
  "scripts": {
    "generate-types": "typesharp",
    "dev": "typesharp && nuxt dev",
    "build": "typesharp && nuxt build"
  }
}

Advanced Examples

1. Records

TypeSharp supports all C# record forms. Records are emitted as TypeScript interfaces identical to classes.

Positional record:

[TypeSharp]
public record ProductSummary(int Id, string Name, decimal Price, bool IsActive);
export interface ProductSummary {
  id: number;
  name: string;
  price: number;
  isActive: boolean;
}

Positional record with nullable and collection parameters:

[TypeSharp]
public record UserRecord(int Id, string? DisplayName, List<string> Tags);
export interface UserRecord {
  id: number;
  displayName: string | null;
  tags: string[];
}

Generic positional record:

[TypeSharp]
public record PagedResult<T>(IEnumerable<T> Items, int TotalCount, int PageSize);
export interface PagedResult<T> {
  items: T[];
  totalCount: number;
  pageSize: number;
}

record class and record struct:

[TypeSharp]
public record class AddressRecord(string Street, string City, string PostalCode);

[TypeSharp]
public record struct CoordRecord(double Lat, double Lng);

Body-only record (no primary constructor):

[TypeSharp]
public record PersonRecord
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
}

Record inheritance:

[TypeSharp]
public record BaseEvent(Guid Id, DateTime OccurredAt);

[TypeSharp]
public record UserCreatedEvent(Guid Id, DateTime OccurredAt, string Email) : BaseEvent(Id, OccurredAt);
export interface BaseEvent {
  id: string;
  occurredAt: string;
}

export interface UserCreatedEvent extends BaseEvent {
  id: string;
  occurredAt: string;
  email: string;
}

Per-parameter attribute overrides (use the property: target — required by C# for primary constructor parameters):

[TypeSharp]
public record SecureRecord(
    string Name,
    [property: TypeIgnore] string Secret,
    [property: TypeAs("Date")] DateTime CreatedAt,
    [property: Obsolete("Use Name")] string LegacyName
);
export interface SecureRecord {
  name: string;
  /** @deprecated Use Name */
  legacyName: string;
  createdAt: Date;
}

[TypeIgnore], [TypeName("x")], [TypeAs("y")], and [Obsolete] on primary constructor parameters require the property: attribute target because C# must know the attribute applies to the generated property, not the constructor parameter itself.

2. With Inheritance

C#:

[TypeSharp]
public class BaseEntity
{
    public int Id { get; set; }
    public DateTime CreatedAt { get; set; }
}

[TypeSharp]
public class Product : BaseEntity
{
    public string Name { get; set; }
    public decimal Price { get; set; }
}

Generated TypeScript:

export interface BaseEntity {
  id: number;
  createdAt: string;
}

export interface Product extends BaseEntity {
  name: string;
  price: number;
}

3. Computed Properties

TypeSharp includes expression-bodied and block getter properties:

C#:

[TypeSharp]
public class PollOptionUserLinkDto
{
    public int Id { get; set; }
    public int UserId { get; set; }
    public UserDto User { get; set; }

    // Expression-bodied
    public string? Avatar => User?.Avatar;

    // Block getter
    public string UserFirstName { get { return User.FirstName; } }
}

Generated TypeScript:

export interface PollOptionUserLinkDto {
  id: number;
  userId: number;
  user: UserDto;
  avatar: string | null;
  userFirstName: string;
}

4. Dictionary Types

C#:

[TypeSharp]
public class PermissionMap
{
    public Dictionary<string, bool> Flags { get; set; }
    public IReadOnlyDictionary<string, List<string>> RolePermissions { get; set; }
}

Generated TypeScript:

export interface PermissionMap {
  flags: Record<string, boolean>;
  rolePermissions: Record<string, string[]>;
}

5. Obsolete / Deprecated Properties

C#:

[TypeSharp]
public class Employee
{
    public int Id { get; set; }
    public string Department { get; set; }

    [Obsolete("Use Department instead.")]
    public string? DepartmentName { get; set; }

    [Obsolete]
    public string? LegacyCode { get; set; }
}

Generated TypeScript:

export interface Employee {
  id: number;
  department: string;
  /** @deprecated Use Department instead. */
  departmentName: string | null;
  /** @deprecated */
  legacyCode: string | null;
}

6. Custom Type Name Override

[TypeSharp("auth_response")]
public class AuthResponse
{
    public string AccessToken { get; set; }
    public string RefreshToken { get; set; }
}
export interface auth_response {
  accessToken: string;
  refreshToken: string;
}

7. Multi-Project

{
  "source": [
    "C:/MyApp/Api/Api.csproj",
    "C:/MyApp/Domain/Domain.csproj",
    "C:/MyApp/Contracts/Contracts.csproj"
  ],
  "outputPath": "./src/types"
}

8. Single Output File

const config: TypeSharpConfig = {
  source: "./Backend/Backend.csproj",
  outputPath: "./src/types",
  singleOutputFile: true,
};

9. Custom Naming Conventions

const config: TypeSharpConfig = {
  source: "./Backend/Backend.csproj",
  outputPath: "./src/types",
  namingConvention: {
    dir: "kebab",
    file: "camel",
  },
};

Type Mappings

Primitives & Common Types

C# Type TypeScript Type
bool boolean
byte, decimal, double, float, int, long number
DateTime, DateOnly, TimeOnly string
Guid, string string
object any

Collections

C# Type TypeScript Type
List<T>, ICollection<T>, IEnumerable<T>, T[] T[]
Dictionary<K, V>, IDictionary<K, V>, IReadOnlyDictionary<K, V> Record<K, V>

ASP.NET / File Types

C# Type TypeScript Type
IFormFile, FormFile File
IFormFileCollection File[]
FileStream, MemoryStream, Stream Blob

Programmatic Usage

import { generate } from "typesharp";

async function generateTypes() {
  await generate("./path/to/config.ts");
}

generateTypes();

Requirements

  • Node.js >= 20
  • TypeScript >= 4.5 (if using TypeScript config)

License

MIT Š Siyavuya Chagi

Author

Siyavuya Chagi (CeeJay)


Built with â¤ī¸ in South Africa đŸ‡ŋđŸ‡Ļ

Release History

VersionChangesUrgencyDate
v0.2.3## v0.2.3 - 2026-05-31 ### Summary Improved generation accuracy, richer terminal output, and two new type mapping features. ### Added - **Region preservation** — `#region` / `#endregion` blocks in C# classes are now carried through to generated TypeScript interfaces as `// #region` comments, preserving logical grouping - **`TimeSpan` support** — maps to `number` (total milliseconds); works with nullable, arrays, and generics automatically ### Fixed - **Terminal output** — logsHigh5/31/2026
v0.2.2## v0.2.2 - 2026-05-20 ### Summary - Improved error visibility with a dedicated logger and resolved a type mismatch bug in the mailer. ### Added - Logger (New logger to make error tracking more easily) ### Fixed - The "from" argument must be of type string. Received an instance of Array ### Removed - C# self-contained test projectHigh5/20/2026
v0.2.1## v0.2.1 - 2026-05-01 ### Summary Added `[Union]` attribute support for enums, generating a `const` object and derived union type instead of a standard TypeScript enum. Also fixed `init` accessor support for C# property parsing. ### Added - **`[Union]` enum support** — Enums decorated with `[TypeSharp]` and `[Union]` now generate a `const` object with `as const` and a union type derived from it, instead of a standard `export enum` - **Comma-syntax attribute support** — `[TypeSharp,High5/1/2026
v0.2.0## v0.2.0 - 2026-04-12 ### Summary Improved output logging and cleaner CLI experience with optimized metrics reporting, silent change detection, and automatic cleanup on uninstall. ### Added - **ESLint integration** — Project-wide code quality checks with TypeScript support - **Prettier formatting** — Automatic code formatting for consistency - **Lint scripts** — `npm run lint`, `npm run lint:fix`, and `npm run format` commands - **Pre-uninstall cleanup** — Automatically removes `High4/12/2026
v0.1.6## v0.1.6 - 2026-04-11 ### Summary Comprehensive test suite completion with full CLI testing and incremental generation validation. ### Added - **CLI module tests** — 14 new test cases validating `init` and `generate` commands - Config file creation in `.ts`, `.js`, and `.json` formats - Missing config file error handling - Empty source array handling - Config overwrite prevention - **Incremental generation validation** — 5 passing tests confirming: - File change deteHigh4/11/2026
v1.0.5## [0.1.5] - 2026-04-07 ### Summary - Minor parser improvements - Minor parser improvements and CLI update ### Added - **C# interface filtering in inheritance** — classes inheriting from C# interfaces (e.g. `IActionResult`, `IDisposable`) no longer emit an `extends` clause in the generated TypeScript output. Only concrete base classes are preserved. Interface detection follows the standard C# naming convention (`I` followed by an uppercase letter). - **C# record support** — all recMedium4/7/2026
v1.0.5-beta.0- Minor improvementsMedium4/7/2026
v1.0.4- Peer dependency fixLow3/21/2026
v1.0.3- version bumpLow3/21/2026
v0.1.1<html> <body> <!--StartFragment--><html><head></head><body><h1>TypeSharp v0.1.1 Release Notes</h1> <p><strong>Released:</strong> 2026-02-19<br> <strong>Type:</strong> Minor release — new features + bug fixes<br> <strong>npm:</strong> <code>npm install @siyavuyachagi/typesharp@0.1.1</code></p> <hr> <h2>What's New</h2> <h3>Computed &amp; Block Getter Properties</h3> <p>TypeSharp can now parse all common C# property declaration styles, not just <code>{ get; set; }</code>. This release addsLow2/19/2026
v0.1.0v0.1.0 — Initial public release (2025-12-19) TypeSharp is a direct C# → TypeScript generator (NOT OpenAPI-based). It scans your ASP.NET Core project for classes/enums decorated with a chosen attribute (default `[TypeSharp]`) and generates TypeScript interfaces/enums while preserving generics, inheritance, file grouping, and naming conventions. Highlights - Direct parsing of C# models (no OpenAPI/Swagger required) - Attribute targeting to select models - Generics, inheritance, and collecLow12/19/2025

Dependencies & License Audit

Loading dependencies...

Similar Packages

VSCode-Local-CopilotNo descriptionmain@2026-06-05
claude-terminalManage multiple Claude Code sessions in a single terminal with tabs, session persistence, multi-project workspaces, and git worktree support.main@2026-06-04
ossatureAn open-source harness for spec-driven code generation.master@2026-06-01
spiderly-websiteSpiderly Website - FAQ, Documentation, Pricing, and License Purchasing for the Core Spiderly Library.master@2026-05-31
swaggieTool for generating TypeScript client code for given Swagger API endpointsv2.3.0

More in Developer Tools

system_prompts_leaksExtracted system prompts from ChatGPT (GPT-5.4, GPT-5.3, Codex), Claude (Opus 4.6, Sonnet 4.6, Claude Code), Gemini (3.1 Pro, 3 Flash, CLI), Grok (4.2, 4), Perplexity, and more. Updated regularly.
mypyOptional static typing for Python
pipThe PyPA recommended tool for installing Python packages.
anthropicThe official Python library for the anthropic API