C# model to the TypeScript model compiler.
-
Write C# Code
public class ModelTests { private readonly TypeScriptGenerator _generator = new(new() { CamelCase = true, DetectionMode = DetectionMode.AutoDetect, }); public enum HttpStatus { None, OK = 200, NotFound = 404, } [TypeScriptGenerator] public class Model<T> { public HttpStatus Status { get; set; } public T TypeRef { get; set; } public T? TypeRef_N { get; set; } public required T TypeRef_R { get; set; } public required T? TypeRef_RN { get; set; } public int CsInt32 { get; set; } public int? CsInt32_N { get; set; } public required int? CsInt32_RN { get; set; } public string? CsString_N { get; set; } public required string CsString_R { get; set; } public required string? CsString_RN { get; set; } } [Fact] public void Test() { var code = _generator.GetCode(); var expected = """ /* Generated by TypeSharpV3 dev-version */ export enum HttpStatus { None = 0, OK = 200, NotFound = 404 } export interface Model<T> { status: HttpStatus; typeRef?: T; typeRef_N?: T; typeRef_R: T; typeRef_RN: T; csInt32: number; csInt32_N?: number; csInt32_RN: number | undefined; csString_N?: string; csString_R: string; csString_RN: string; } """; Assert.Equal(expected, code); } }
If TypeScriptModelAttribute not set. the namespace is same as the class Namespace。
-
Compile to TypeScript code:
var builder = new TypeScriptModelBuilder(); builder.CacheType<Cls>(); var tscode = builderCompile();
-
Define class:
public class Cls { public int Value { get; set; } }
-
Compile to TypeScript code:
var builder = new TypeScriptModelBuilder(); builder.CacheType<Cls>(); var tscode = builder.Compile();
If TypeScriptModelAttribute not set. the namespace is same as the class Namespace。
/* Generated by TypeSharp v0.4.0.0 */
declare namespace App {
interface Cls {
value? : number;
}
}