Last active
February 5, 2021 23:35
-
-
Save joeriks/4655063 to your computer and use it in GitHub Desktop.
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<#@ 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"; | |
} | |
#> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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)