Convert C# DTO source files into TypeScript interfaces and enums.
TypeSharp is a Node-powered TypeScript CLI. It reads .cs files directly, parses simple DTO models, and writes one .ts file per DTO or enum.
Run TypeSharp with Deno:
deno run -A src/main.ts \
--source "./samples/SampleModels" \
--file-filter "*.cs" \
--destination "./samples/SampleModels/Typescript" \
--export-moduleOr use a config file:
deno run -A src/main.ts --config "./typesharp.json"With node (tsx):
npx tsx src/main.tsWith node (build the project then run with node)
npm run build && node ./dist/main.jsWhen --config is not provided, TypeSharp reads typesharp.json from the current folder if it exists. Command-line options override config file values.
-c, --config optional
Path to a JSON configuration file. Default: ./typesharp.json when that file exists.
-s, --source required unless set in config
Folder containing .cs files. Subfolders are included.
-f, --file-filter optional
Glob-style filter for source files, such as *.cs or *Dto.cs. Default: *.cs.
-t, --type-filter optional
Glob-style filter for parsed type names, such as Person*. Default: all parsed types.
-d, --destination required unless set in config
Folder where generated TypeScript files are written.
-w, --watch optional
Watches the source folder and regenerates when .cs files change. Default: false.
-m, --export-module optional
Creates an aggregate module file that re-exports generated files. Default: false.
--dictionary-style optional
Controls dictionary output. Options: index-signature, record. Default: index-signature.
--readonly-properties optional
Emits all interface properties as readonly. Default: false.
--quote-style optional
Controls generated string quotes. Options: double, single. Default: double.
--semicolons, --no-semicolons optional
Controls semicolons on generated TypeScript statements. Options: true, false. Default: true.
Create typesharp.json:
{
"source": "./samples/SampleModels",
"fileFilter": "*.cs",
"typeFilter": "*",
"destination": "./samples/SampleModels/Typescript",
"watch": false,
"exportModule": true,
"dictionaryStyle": "record",
"readonlyProperties": false,
"quoteStyle": "double",
"semicolons": true
}Override config values from the command line:
deno run -A src/main.ts \
--config "./config/tssharp.json" \
--dictionary-style record \
--readonly-properties \
--quote-style single \
--no-semicolonsTypeSharp supports DTO-focused C# source files:
namespacepublic class- generic classes
- simple inheritance
public enum- attributes
- public auto-properties
- primitive types
objectDateTimeandDateTimeOffset- nullable shorthand, such as
int? - arrays
List<T>and other common collection interfacesDictionary<TKey, TValue>- nested generics
- custom DTO and enum references parsed from source
TypeSharp recognizes these attributes on properties:
[Optional]emits an optional TypeScript property.[Nullable]adds| null.[UnknownObject]emitsunknown.[Partial]wraps the property type inPartial<T>.[TypeUnion(...)]emits literal unions.[Readonly]or[ReadOnly]emits a readonly property.
TypeSharp also recognizes these attributes on classes:
[Readonly],[ReadOnly], or[ReadonlyProperties]emits all properties on that interface as readonly.
C# input:
namespace SampleModels.Models
{
[Readonly]
public class Person
{
public string Name { get; set; }
public Dictionary<string, Address> Addresses { get; set; }
[Optional]
[TypeUnion("admin", "user")]
public string Role { get; set; }
}
}Generated TypeScript with dictionaryStyle set to record:
import { Address } from "./Address";
export interface Person {
readonly name: string;
readonly addresses: Record<string, Address>;
readonly role?: "admin" | "user";
}Aggregate module output:
export * from "./Address";
export * from "./Person";Test:
npm rum test