public
Last active

Generate Typescript Interfaces for all types (classes) within one assembly. 1. Add reference to your assembly within a project. 2. Create somename.tt file with below contents. 3. Insert your own assembly name in the tt file 4. Get the assembly with one of the types 5. Save the tt and the generator will create a interfacefile for you (somename.d.ts)

  • Download Gist
sample.d.ts
TypeScript
1 2 3 4 5 6 7 8 9 10 11 12 13
// Sample generation:
//
// Types in assembly: MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// Generated 01/28/2013 13:11:37
//
module myInterfaces {
//
// Type: MyAssembly.Models.Client
//
export interface Client {
Name: string; // System.String
...
typescriptInterfaces.tt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
<#@ template debug="false" hostspecific="true" language="C#" #>
<# // 1. Insert your own assemblies here (+ they need to be referenced from your project) #>
<#@ assembly name="$(TargetDir)\MyAssembly.dll" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Web" #>
<#@ import namespace="System.Text.RegularExpressions" #>
<#@ import namespace="System.Threading.Tasks" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".d.ts" #>
<#@ assembly name="$(TargetPath)" #>
<#
 
//
// Get the assembly of one of the types:
//
var assembly = typeof(MyAssembly.Models.SomeType).Assembly;
var types = assembly.GetTypes(); // add a filter if you like : assembly.GetTypes().Where(t=>t.FullName.StartsWith("MyAssembly.SomeNameSpace"));
#>
//
// Types in assembly: <#= assembly.FullName #>
// Generated <#= DateTime.Now #>
//
module myInterfaces {
<#
 
foreach (var type in types)
{
 
#>
//
// Type: <#= type.FullName #>
//
export interface <#= type.Name #> {
<#
 
foreach (var property in type.GetProperties())
{
 
#>
<#= getTypeScriptString(property) #>
<#
}
 
#>
}
<#
 
} // types
 
#>
}
<#+
string getTypeScriptString(PropertyInfo propertyInfo) {
var typeString = getTypesScriptType(propertyInfo.PropertyType.ToString());
if (propertyInfo.PropertyType.ToString().Contains("Nullable"))
{
return string.Format("{0}?: {1}; // {2}", propertyInfo.Name, typeString, propertyInfo.PropertyType);
}
else
{
return string.Format("{0}: {1}; // {2}", propertyInfo.Name, typeString, propertyInfo.PropertyType);
}
}
string getTypesScriptType(string propertyType) {
switch (propertyType)
{
case "System.String":
return "string";
case "System.Int32":
return "number";
case "System.Boolean":
return "bool";
case "System.Nullable`1[System.Boolean]":
return "bool";
case "System.Nullable`1[System.Int32]":
return "number";
default:
break;
}
return "any";
}
#>

refs/thanks: http://typescript.codeplex.com/discussions/406685 (Hacked together C# to TypeScript Interface Generator by JonKragh) and https://gist.github.com/4583549 (T4 template that creates Typescript type definitions for all your Signalr hubs by Robfe)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.