Introduction
TypeScripter is a .NET class library used to generate TypeScript definitions for .NET types.
Basic Usage
Code
public class Animal
{
public string Age
{
get;
set;
}
public void Sleep(int hours)
{
}
}
var scripter = new TypeScripter.Scripter();
var output = scripter
.AddType(typeof(Animal))
.ToString();
Output
declare module TypeScripter.Examples {
interface Animal {
Age: string;
Sleep(hours: number): void;
}
}
Inheritance
Code
public class Mammal : Animal
{
public void WarmBlood()
{
}
}
var scripter = new TypeScripter.Scripter();
var output = scripter
.AddType(typeof(Mammal))
.ToString();
Output
declare module TypeScripter.Examples {
interface Mammal extends TypeScripter.Examples.Animal {
WarmBlood(): void;
}
interface Animal {
Age: string;
Sleep(hours: number): void;
}
}
Generics
Code
public class Zoo<T> where T : Animal
{
public T[] GetAnimals()
{
return default(T[]);
}
}
public class MammalZoo : Zoo<Mammal>
{
}
var scripter = new TypeScripter.Scripter();
var output = scripter
.AddType(typeof(MammalZoo))
.ToString();
Output
declare module TypeScripter.Examples {
interface MammalZoo extends TypeScripter.Examples.Zoo<Typescripter.Examples.Mammal> {
}
interface Zoo<T extends Typescripter.Examples.Animal> {
GetAnimals(): T[];
}
interface Animal {
Age: string;
Sleep(hours: number): void;
}
interface Mammal extends TypeScripter.Examples.Animal {
WarmBlood(): void;
}
}
Type Readers
Code
[DataContract]
public class ZooKeeper
{
[DataMember]
public string Name
{
get;
set;
}
}
[ServiceContract]
public class ZooService
{
[OperationContract]
public ZooKeeper GetKeeper()
{
return default(ZooKeeper);
}
}
var assembly = this.GetType().Assembly;
var scripter = new TypeScripter.Scripter();
var output = scripter
.UsingTypeReader(
new TypeScripter.Readers.CompositeTypeReader(
new TypeScripter.Readers.DataContractTypeReader(),
new TypeScripter.Readers.ServiceContractTypeReader()
)
)
.AddTypes(assembly)
.ToString();
Output
declare module TypeScripter.Examples {
interface ZooKeeper {
Name: string;
}
interface ZooService {
GetKeeper(): TypeScripter.Examples.ZooKeeper;
}
}
Formatters
Code
public class PromiseFormatter : TsFormatter
{
public override string Format(TsType returnType)
{
return string.Format("ng.IPromise<{0}>", base.Format(returnType));
}
}
var assembly = this.GetType().Assembly;
var scripter = new TypeScripter.Scripter();
var output = scripter
.UsingTypeReader(
new TypeScripter.Readers.CompositeTypeReader(
new TypeScripter.Readers.DataContractTypeReader(),
new TypeScripter.Readers.ServiceContractTypeReader()
)
)
.UsingFormatter(new PromiseFormatter())
.AddTypes(assembly)
.ToString();
Output
declare module TypeScripter.Examples {
interface ZooKeeper {
Name: string;
}
interface ZooService {
GetKeeper(): ng.IPromise<Typescripter.Examples.Zookeeper>;
}
}