This small library generates TypeScript type definition based on C# types. Use it directly in your backend C# project to generate code for your frontend TypeScript project. You can also wrtie small console app, to generate code by pre-build tools.
Works on Full & NET Core framework!
Install by nuget: Install-Package TypeScriptBuilder
- Resolving type dependency
- Generics
- Type inheritance
- Namespaces (modules)
- Enums
- Nullable types
- Dictionary converison (to strong type TS indexed objects)
- Set of code generation control attributes
anyfor types that can't be converted
varts=newTypeScriptGenerator();// add types you need (dependant types will be added automatically)ts.AddCSType(typeof(User));// get generated code as stringts.ToString();// or write to filets.Store("Test.ts");Sample C# class:
classUser{publicintId;publicstringLogin;[TSExclude]// exclude field, can be applied on class toopublicboolActive;}Output TypeScript:
exportinterfaceIUser{Id: number;Login: string;}Dictionaries with keys of type int or string will be translated to strong typed TS indexed objects:
classEntity<T>{publicTValue;}classTest{publicDictionary<int,Entity<DateTime>>Repo;}exportinterfaceIEntity<T>{Value: T;}exportinterfaceITest{Repo: {[index: number]: IEntity<Date>};}You can annotate code with below attributes to affect TS code generation.
Can be applied on classes, stucts or fields/properties - these items will be omited during TS code generation.
Note: You can also exclude types by using method ExcludeType.
When applied on field/property it's type will be set to any and skips fruther type analizes.
Can be used on class, struct or enum to rename generated type:
[TSMap("Funky")]classMyCSharpClass{}classTest{publicMyCSharpClassInstance;}exportinterfaceFunky{}exportinterfaceTest{Instance: Funky;}If applied on class (B), all fields from base classe (A) are included in class (B):
classA{publicintId;publicboolActive;}[TSFlat]classB:A{publicstringName;}exportinterfaceB{Id: number;// from AActive: boolean;// from AName: string;}TypeScriptGenerator exposes following methods:
TypeScriptGenerator AddCSType(Type type): add C# type to generate (dependency types will be added automatically)string TypeName(Type type): get TypeScript type name, if requested type requires declaration, it will be automatically addedstring ToString(): gets string with generated type declarations (all namespaces combined)void Store(string file): stores declarations in to the fileTypeScriptGenerator ExcludeType(Type type): exclude type
Simply use above methods according to your needs, then use ToString to get generated type declarations.
You can also use CodeTextBuilder helper class that is used to build nicely formatted code.
You can pass options TypeScriptGenerator constructor:
UseCamelCase: changes field names formMyTestFieldtomyTestField(default true)EmitIinInterface: adds I in interface names,MySimpleDatabecomesIMySimpleData(default true)EmitReadonly: addsreadonlyto readonly fields, requires TypeScript 2.0 (default true)EmitComments: adds comments with oryginal C# type description (default false)
To learn more run Test project in the solution.