配置强化类型以添加​​自定义打字稿交集类型

configuring reinforced-typings to add custom typescript intersection type

我正在使用 reinforced.typings 流畅配置 API(胸腔方法)将我的 C# DTO 映射到 typescript 接口,生成单个文件。

目前我得到(正确的)输出,例如

export interface ICourseDto
{
   start: Date;
   facultyMeetingRoom: IRoomDto;
   courseParticipants: ICourseParticipantDto[];
   ...
}
export interface IRoomDto ...

理想情况下,我想:

  1. import breeze from 'breeze-client'添加到生成的打字稿文件的开头
  2. 改变复杂属性的类型,使上面的输出变成

-

export interface ICourseDto
{
    start: Date;
    facultyMeetingRoom: IRoomDto & breeze.Entity;
    courseParticipants: (ICourseParticipantDto & breeze.Entity)[];
    ...

使用 reinforced.typings 流畅的配置是否可能,如果可以,我需要什么配置代码来实现它?

实现您想要的最简单方法是(其中 s 是 ConfigurationBuilder):

s.Global(a => a.UseModules());
s.AddImport("breeze", "breeze-client");

var mySpecialTypes = typeof(IBreezeEntity).Assembly.GetTypes()
    .Where(d => typeof(IBreezeEntity).IsAssignableFrom(d));

foreach (var type in mySpecialTypes)
{
    s.Substitute(type, new RtSimpleTypeName($"I{type.Name} & breeze.Entity"));
}

Reinforced.Typings 也保留继承。考虑从 common type/interface 派生实体并将其导出。