Search code examples
reinforced-typings

configuring reinforced-typings to add custom typescript intersection type


I am using the reinforced.typings fluent configuration API (pleural methods) to map my C# DTOs to typescript interfaces, generating a single file.

currently i am getting the (correct) output such as

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

ideally, I would like to:

  1. add import breeze from 'breeze-client' to the beginning of the generated typescript file
  2. alter the type of the complex properties, such that the output above would become

-

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

is this possible with the reinforced.typings fluent configuration, and if so, what configuration code do I need to achieve it?


Solution

  • The simpliest way to achieve what you want is (where s is 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 also preserves inheritance. Consider deriving your entities from common type/interface and exporting it also.