DTO to TypeScript generator

C#Typescript

C# Problem Overview


I have a C# library (assembly) which contains a set of DTOs which I use to populate my knockout models (TypeScript).

I would like to make sure that the mapping between the JSON data and the ViewModel is correct.

Are there a tool which can generate TypeScript classes from my C# classes? Something like T4 or similar (or just a command line tool which is run as a POST-build event)?

(Note that the TypeScript files must be placed in another project than the DTO assembly)

C# Solutions


Solution 1 - C#

There are lots of projects that do this. Your best bet today will be to assess which option is still actively maintained, and meets your requirements 

Note

The world is strongly going TypeScript for both front and backend. Hence the diverse state of cross language tooling.

Solution 2 - C#

Check out the open-source project NSwag: With the GUI, you can select a .NET class from an existing assembly and generate the TypeScript interface for it.

screenshot

There is also a command line tool and support for T4 templates as well as generation of client code for Web API controllers...

Solution 3 - C#

There is a new library called TypeScriptD which will create .d.ts files for your .NET classes.

This can be run as a command and works for .NET or winmd.

Solution 4 - C#

I had a similar need but found the existing projects were too limiting in one way or another (buggy, no generics, no methods, etc.) so I ended up rolling my own (http://cjlpowers.github.io/TypeScripter/). In my case I was dealing with ServiceContract/DataContract types on the server and needed .d.ts files to aid client side development. The solution is extensible should you need to tweak it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjgauffinView Question on Stackoverflow
Solution 1 - C#basaratView Answer on Stackoverflow
Solution 2 - C#Rico SuterView Answer on Stackoverflow
Solution 3 - C#FentonView Answer on Stackoverflow
Solution 4 - C#cpowersView Answer on Stackoverflow