Ian Ringrose Ian Ringrose -4 years ago 113
TypeScript Question

End to end type safety of JSON between C# and typescript

Both Typescript and C# supports type safe access to the members of an object checked at “compile” type, and json is used to transport “data transfer objects” between web application back ends and their front end.

I would expect here to be tooling that could generate typescript “wrapper classes” from the C# classes that defined the JSON messages – yet I have never heard about such tooling being used.
It may be that not being an active TypeScript developer I have missed something in the TypeScript toolset for doing this, or that one of the “standard” JSON toolkits for .net already does it. Or I could just be asking a very daft question…..

Answer Source

TypeLITE will convert C# classes into TypeScript interfaces:

public class Address {
    public string Street { get; set; }
}

public class Person {
    public string Name { get; set; }
    public List<address> Addresses { get; set; }
}

Becomes:

interface Address {
    Street: string;
}

interface Person {
    Name: string;
    Addresses: Address[];
}

This would keep your TypeScript honest when consuming a C# API.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download