Auto Generate TypeScript Data Transaction Interface from C # JSON Service Implementation

Our C # application provides a series of JSON web services for use by the web application client. We are currently creating this JSON intelligently by hand using recursive object-to-JSON-dictionary calls.

We are gradually embedding parts of our web application in TypeScript - we would like to have TypeScript interfaces for defining the form of JSON data that client code should expect from the server.

Is there a way to automatically generate TypeScript data transaction interface definitions at compile time based on the C # JSON service implementation?

For example, the C # code that generates JSON currently looks something like this:

    public class ConcreteObj
    {
        public string SimpleProperty;
        public ConcreteObj[] Children;
    }

    public static IDictionary<string, object> ConvertToScriptValue(ConcreteObj obj)
    {
        IDictionary<string, object> result = new Dictionary<string, object>();

        result["simpleProperty"] = obj.SimpleProperty; // string
        result["children"] = Array.ConvertAll(obj.Children, ConvertToScriptValue); // ConcreteObj array

        return result;
    }

      

We would like to see TypeScript like this:

interface ConcreteObj {
    simpleProperty: string;
    children: ConcreteObj[];
}

      

+3


source to share





All Articles