DTO to TypeScript generator [closed]

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)


Solution 1:

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 🌹

  • TypeLITE : http://type.litesolutions.net/
  • TypeScriptModelsGenerator : https://www.nuget.org/packages/TypeScriptModelsGenerator
  • NSwag : https://github.com/RicoSuter/NSwag
  • ToTypeScriptD : https://github.com/ToTypeScriptD/ToTypeScriptD
  • TypeScripter : http://cjlpowers.github.io/TypeScripter/
  • MTT : https://github.com/CodySchrank/MTT
  • TypeGen : https://github.com/jburzynski/TypeGen

Note

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

Solution 2:

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:

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:

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.