TypeScript Type Generator
TypeScript type generator is a simple library that generates TypeScript interfaces, classes and enums based on .NET types. It does support generics, inheritance, nullable value types and also it could handle string/number based dictionaries as associative arrays.
The binaries are available through the nuget feed - TypeScript.NET.
Usage scenario
The main usage scenario is to mirror to TypeScript world data objects defined on the .NET server side, this is why the generator does not handle any methods/properties with a body. Although it supports auto-properties and sets default values for primitive types such as number, boolean, string or enum.
For example, based on the following C# type declaration:
public class DerivedClass<T1, T2> : BaseClass, IGenericInterface<int, T1>, ISimpleInterface
{
public IDictionary<int, T1> Map { get; set; }
public ICollection<T2> GenericData { get; set; }
public string Code { get; set; }
public DerivedClass()
{
this.Code = "The Code";
}
}
the generator produces the following TypeScript definition:
export class DerivedClass<T1, T2> extends BaseClass implements IGenericInterface<number, T1>, ISimpleInterface {
Map: Common.IMapOfNumber<T1>;
GenericData: T2[];
Code: string;
constructor() {
super();
this.Map = {};
this.GenericData = [];
this.Code = "The Code";
}
}
Usage
Currently there are two approaches to generate TypeScript files:
- Directly using TypeScriptGenerator class
- Using MsBuild target
In both cases you need to pass entity information about the source types to be used, destination location for TypeScript code and custom configuration if you need to customize generation logic.
Generate from code
In order to generate TypeScript code from your application, you need to reference TypeScript.NET nuget package.
You could generate TypeScript definition based on specific types:
var generator = new TypeScriptGenerator();
var typeScriptCode = generator.GenerateFromTypes(
typeof(ISimpleInterface),
typeof(MyDataModel),
typeof(MyEnum));
...based on all types from a specific assembly:
var generator = new TypeScriptGenerator();
var typeScriptCode = generator.GenerateFromAssembly(
Assembly.FromFile("MyAssembly.dll"));
...or based on all types from specific assembly which are decorated by a special attribute:
var generator = new TypeScriptGenerator();
var typeScriptCode = generator.GenerateFromAssembly<GenerateTypeScriptAttribute>(
Assembly.FromFile("MyAssembly.dll"));
And if you would want to override some generation behavior you need to pass configuration object into TypeScriptGenerator constructor. Refer to Configuration section if you want to know which parts of code generation are configurable.
Generate using MsBuild task
You might want to use this version if you would want to produce new versions of TypeScript definitions during each build automatically. To do that you could use the dedicated MsBuild task that could be registered as an additional build target.
Configuration
This is the full list of parameters you could configure:
- UseAssociativeArray - Indicates whether to use a special logic to create an associated array interface for IDictionary<string, T> or IDictionary<number type, T> types.
- StringMapInterfaceName - Fullname of TypeScript interface for string based generic associate array.
- NumberMapInterfaceName - Fullname of TypeScript interface for number based generic associate array.
Pending
- Static fields/auto-properties
- Const fields
- Empty properties/methods