cs2ts-sf9
v1.0.4
Published
A powerful CLI tool that converts C# model classes to TypeScript interfaces with full support for inheritance, class references, and intelligent naming conventions.
Maintainers
Readme
CS2TS Simplify9 - C# to TypeScript Converter
A powerful CLI tool that converts C# model classes to TypeScript interfaces with full support for inheritance, class references, and intelligent naming conventions.
🚀 Features
- 🔄 Complete Type Conversion: Converts C# classes and interfaces to TypeScript interfaces
- 📦 Inheritance Support: Handles class inheritance (
Admin : User→Admin extends User) - 🔗 Cross-File References: Automatically generates imports for type dependencies
- 📝 Smart Naming: Converts PascalCase to camelCase with intelligent acronym handling
- 📁 Same-File Grouping: Multiple C# types in one file generate one TypeScript file
- 🧠 Registry-Aware: Distinguishes between primitive types and custom classes
- 📋 Collection Support:
List<T>,IEnumerable<T>,ICollection<T>→T[]
📦 Installation
Global Installation
npm install -g cs2ts-sf9Local Installation
npm install --save-dev cs2ts-sf9🛠 Usage
CLI Command
cs2ts-sf9 convert -s <source-directory> -o <output-directory>Options
-s, --source <path>: Source directory containing C# files (default:./)-o, --out <path>: Output directory for TypeScript files (default:./types)-h, --help: Display help information-V, --version: Display version number
Examples
Basic conversion:
cs2ts-sf9 convert -s ./Models -o ./src/typesUsing default directories:
cs2ts-sf9 convertFrom package.json script:
{
"scripts": {
"generate-types": "cs2ts-sf9 convert -s ./backend/Models -o ./frontend/src/types"
}
}📝 Input/Output Examples
C# Input Files
Models/User.cs
public class User
{
public Guid Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public DateTime CreatedDate { get; set; }
public bool IsActive { get; set; }
}
public interface IRepository
{
Guid Id { get; set; }
void Save();
}
public class Admin : User
{
public string Role { get; set; }
public List<string> Permissions { get; set; }
}Models/Product.cs
public class Product
{
public Guid Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public Item MainItem { get; set; }
public List<Item> RelatedItems { get; set; }
public User CreatedBy { get; set; }
}
public class Item
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}TypeScript Output Files
types/User.ts
export interface User {
id: string;
firstName: string;
lastName: string;
email: string;
createdDate: string;
isActive: boolean;
}
export interface IRepository {
id: string;
}
export interface Admin extends User {
role: string;
permissions: string[];
}types/Product.ts
import { User } from "./User";
export interface Product {
id: string;
name: string;
price: number;
mainItem: Item;
relatedItems: Item[];
createdBy: User;
}
export interface Item {
id: string;
name: string;
description: string;
}🧠 Intelligent Features
Smart Naming Conversion
CS2TS intelligently converts C# PascalCase properties to TypeScript camelCase:
| C# Property | TypeScript Property | Notes |
| ---------------- | ------------------- | ---------------------------- |
| FirstName | firstName | Standard conversion |
| ID | id | Acronym handling |
| URL | url | Acronym handling |
| XMLData | xmlData | Leading acronym + PascalCase |
| IsHTTPSEnabled | isHTTPSEnabled | Mixed case handling |
Supported Acronyms: ID, URL, URI, XML, HTML, HTTP, HTTPS, API, JSON, SQL, UI, UX
Type Mapping
| C# Type | TypeScript Type | Notes |
| --------------------------------------------- | ------------------ | -------------------------- |
| int, float, double, decimal | number | All numeric types |
| string | string | Direct mapping |
| bool | boolean | Boolean conversion |
| DateTime, Guid | string | Serialization assumption |
| List<T>, IEnumerable<T>, ICollection<T> | T[] | Generic collections |
| CustomClass | CustomClass | Preserved for known types |
| int?, string? | number, string | Nullable types (? removed) |
Import Generation
- Cross-file references: Automatically generates imports when types reference classes from other files
- Same-file optimization: Types in the same C# file don't import each other
- Relative paths: Uses relative imports (
./User,./Models) - Sorted imports: Alphabetically ordered for consistency
🏗 Architecture
CS2TS uses a two-pass parsing architecture:
- First Pass: Scans all C# files to build registries of classes and interfaces
- Second Pass: Processes inheritance relationships and resolves type dependencies
Supported C# Features
- ✅ Public classes and interfaces
- ✅ Class inheritance (
class Admin : User) - ✅ Interface inheritance (
interface IUserRepo : IRepository) - ✅ Auto-properties with
{ get; set; }syntax - ✅ Generic collections (
List<T>,IEnumerable<T>) - ✅ Nullable types (
string?,int?) - ✅ Multiple types per file
Limitations
- ❌ Method signatures (interfaces only extract properties)
- ❌ Custom property accessors (only auto-properties)
- ❌ Attributes/annotations
- ❌ Generic type constraints
- ❌ Nested classes
🔧 Configuration
Currently, CS2TS works with zero configuration. Future versions may include:
- Custom type mappings
- Naming convention options
- Output format preferences
- Include/exclude patterns
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Development Setup
git clone https://github.com/fawzi-shiyyab19/cs2ts-sf9.git
cd cs2ts-sf9
npm install
npm run buildTesting
# Run the built CLI
npm run build
node dist/index.js convert -s ./test-models -o ./test-output
# Development mode
npm run start -- convert -s ./test-models -o ./test-output📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🐛 Issues
If you encounter any issues or have feature requests, please create an issue on GitHub.
📈 Changelog
v1.0.0
- Initial release
- C# to TypeScript conversion
- Inheritance support
- Automatic import generation
- Smart naming conventions
- Same-file type grouping
Made with ❤️ for developers working with C# and TypeScript
