ฉันชอบการแก้ปัญหาของ citykid ฉันได้ขยายมันออกไปเล็กน้อย ดังนั้นการแก้ปัญหายังขึ้นอยู่กับเทคนิคการสร้างโค้ดด้วยเทมเพลต T4
สามารถสร้างประเภท TypeScript ทั่วไปและการประกาศโดยรอบ
สนับสนุนการสืบทอดและการใช้งานอินเทอร์เฟซ
รองรับ generics อาร์เรย์และรายการเป็นฟิลด์ประเภท
นอกจากนี้ยังแปลเป็นประเภท TypeScript ที่ไม่ได้กล่าวถึงอย่างชัดเจนในการกำหนดค่า (ตัวอย่างเช่นเรานำเข้าประเภท A และในเอาต์พุต TS คุณจะพบประเภทอื่น ๆ ได้แก่ ประเภทของฟิลด์ประเภทฐานและอินเทอร์เฟซ)
คุณยังสามารถแทนที่ชื่อประเภทได้
นอกจากนี้ยังรองรับ Enums
ตัวอย่างการใช้งาน (คุณสามารถค้นหาได้ในที่เก็บโครงการ):
// set extension of the generated TS file
<#@ output extension=".d.ts" #>
// choose the type of TS import TsMode.Ambient || TsMode.Class
<# var tsBuilder = new TsBuilder(TsMode.Ambient); #>
// reference assembly with the c# types to be transformed
<#@ assembly name="$(SolutionDir)artifacts\...\CsT4Ts.Tests.dll" #>
// reference namespaces
<#@ import namespace="CsT4Ts.Tests" #>
<#
//add types to processing
tsBuilder.ConsiderType(typeof(PresetDTOBase), "PresetBase");
tsBuilder.ConsiderType(typeof(PresetDTO), "Preset");
tsBuilder.ConsiderType(typeof(TestInterface<,>));
#>
// include file with transformation algorithms
<#@ include file="CsT4Ts.t4" #>
และคุณจะได้ผลลัพธ์
//CsT4Ts.Tests.PresetDTOBase => PresetBase
// CsT4Ts.Tests.PresetDTO => Preset
// CsT4Ts.Tests.TestInterface`2 => TestInterface
// CsT4Ts.Tests.TestEnum => TestEnum
declare class PresetBase
{
PresetId: string;
Title: string;
InterviewDate: string;
}
declare class Preset extends PresetBase
{
QuestionsIds: string[];
}
declare interface TestInterface<TA, TB>
{
A: string;
B: number;
C: TestEnum;
D: TestEnum[];
E: number[];
F: TA;
G: TB[];
}
declare enum TestEnum
{
Foo = 10,
Boo = 100
}
ตรวจสอบโซลูชันฉบับเต็มได้ที่นี่: https://bitbucket.org/chandrush/cst4ts