Typewriter Optional Members from Nullable or Optional c# members

282 Views Asked by At

This Typewriter script

${
  string IsOptional(Property p)
  {
    if (p.Type.IsNullable || p.Attributes.Any(a => a.Name == "Optional")) 
     return "?";
    else return "";
  }

}$Classes(*)[
  export interface $Name { $Properties[
  $name$IsOptional: $Type;]
}]

will produce

export interface Foo { 
   prop1: string;
   prop2?: string;
   prop3?: number;
}

from c# class/file

[AttributeUsage(AttributeTargets.All)]
public class OptionalAttribute : Attribute
{
}
//
public class Foo
{
  public string prop1; {get; set;}
  [Optional]
  public string prop2 {get; set}
  public decimal? age {get; set}
}

Paying it forward and wondering if it can be improved? Seems like Typewriter should handle the Nullable members automatically.

0

There are 0 best solutions below