TSCodegen 1.8.0

The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package TSCodegen --version 1.8.0                
NuGet\Install-Package TSCodegen -Version 1.8.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TSCodegen" Version="1.8.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TSCodegen --version 1.8.0                
#r "nuget: TSCodegen, 1.8.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install TSCodegen as a Cake Addin
#addin nuget:?package=TSCodegen&version=1.8.0

// Install TSCodegen as a Cake Tool
#tool nuget:?package=TSCodegen&version=1.8.0                

TSCodegen

Library for C# to TypeScript type conversion. Available as NuGet package.

Compatible with .NET Standard 2.0 and higher!

Usage

Library contains two classes - TypeScriptType and TypeScriptTypes. The first is for creating primitive types and interface names. The second is for creating declarations from classes and enums.

Conversion table:

  • Void → void
  • Boolean → boolean
  • Numerics → number
  • Char & string → string
  • DateTime → Date | DateTimeString*
  • TimeSpan → TimeString*
  • IFormFile → File
  • Dynamic types → any
  • All other types → unknown

Generic types:

  • Array<T> / IEnumerable<T> → T[]
  • Dictionary<T, U> → Record<T, U>
  • Nullable<T> → T | null
  • Task<T> → T

<sub>* DateTimeString and TimeString should be described in a *.d.ts file.</sub>

DateTimeString and TimeString declaration example

declare type DateString = "YYYY-MM-DD";
declare type TimeString = "HH:mm:ss";
declare type DateTimeString = `${DateString}T${TimeString}`;

TypeScriptType

// Base properties
public string BaseTypeName { get; private set; }
public bool IsPrimitive { get; private set; }
public bool IsClass { get; private set; }
public bool IsEnum { get; private set; }
public bool IsArray { get; private set; }
public bool IsDictionary { get; private set; }
public bool IsNullable { get; private set; }
// Additional properties
public TypeScriptType Parent { get; private set; }
public TypeScriptType Element { get; private set; }
public TypeScriptType DictionaryKey { get; private set; }
public List<TypeScriptType> GenericArguments { get; private set; }
public List<TypeScriptType> OpenGenericArguments { get; private set; }
public Dictionary<string, string> Values { get; private set; }
public Dictionary<string, TypeScriptType> Properties { get; private set; }
// Constructor argument
public Type CSharpType { get; private set; }
// Helpers
public bool HasParent => Parent != null;
public bool HasElement => Element != null;
public bool IsGeneric => GenericArguments.Count > 0;
public bool HasDeclaration => IsEnum || IsClass;
public override string ToString() => GetFullTypeName();
// Methods
public TypeScriptType(Type type);
public string GetOpenGenericTypeName();
public string GetFullTypeName();
public List<string> GetDeclaration(int indentationSize, bool export = false);

TypeScriptTypes

// Base properties
public List<TypeScriptType> Items { get; }
// Methods
public void Add(TypeScriptType typeScriptType);
public void Add(IEnumerable<TypeScriptType> typeScriptTypes);
public List<string> GetDeclarations(int indentationSize, bool export = false);

<sub>* The Add method will recursively add all related to the argument types.</sub>

Example Application 1

using TSCodegen;

var num = 123;
var str = "qwe";
var flag = true;

Console.WriteLine(new TypeScriptType(num.GetType()));
Console.WriteLine(new TypeScriptType(str.GetType()));
Console.WriteLine(new TypeScriptType(flag.GetType()));

The output will be:

number
string
boolean

Example Application 2

using TSCodegen;

Console.WriteLine(new TypeScriptType(typeof(int)));
Console.WriteLine(new TypeScriptType(typeof(string)));
Console.WriteLine(new TypeScriptType(typeof(bool)));

The output will be:

number
string
boolean

Example Application 3

using TSCodegen;

var type = typeof(MyClass);
var tsType = new TypeScriptType(type);
var strings = tsType.GetDeclaration(4);
Console.WriteLine(string.Join("\n", strings));

class MyClass
{
    public int FieldA;
    public string FieldB;
    private bool FieldC;
}

The output will be:

interface IMyClass {
    fieldA: number;
    fieldB: string;
}

Example Application 4

using TSCodegen;

var tsType = new TypeScriptType(typeof(MyEnum));
var strings = tsType.GetDeclaration(4);
Console.WriteLine(string.Join("\n", strings));

enum MyEnum
{
    Foo = 1,
    Bar = 2,
    Baz = 4,
}

The output will be:

enum MyEnum {
    Foo = 1,
    Bar = 2,
    Baz = 4,
}

Example Application 5

using TSCodegen;

var type = typeof(MyGenericClass<bool, int, string>);
var tsType = new TypeScriptType(type);
Console.WriteLine(tsType);

class MyGenericClass<A, B, C>
{
    public A FieldA;
    public B FieldB;
    public C FieldC;
}

The output will be:

IMyGenericClass<boolean, number, string>

Example Application 6

using TSCodegen;

var type = typeof(MyGenericClass2<,>);
var tsType = new TypeScriptType(type);
var tsTypes = new TypeScriptTypes();
tsTypes.Add(tsType);
var strings = tsTypes.GetDeclarations(4);
Console.WriteLine(string.Join("\n", strings));

class MyGenericClass1<T>
{
    public T FieldA;
}

class MyGenericClass2<T, U> : MyGenericClass1<U>
{
    public T FieldB;
}

The output will be:

interface IMyGenericClass1<T> {
    fieldA: T;
}

interface IMyGenericClass2<T, U> extends IMyGenericClass1<U> {
    fieldB: T;
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated