frhagn / Typewriter

Automatic TypeScript template generation from C# source files
http://frhagn.github.io/Typewriter
Apache License 2.0
537 stars 132 forks source link

Nullable support #310

Open wonea opened 5 years ago

wonea commented 5 years ago

Is there any support within TypeWriter for nullable? int?

I've coded round this in my transforms for now.

GJGowey-biBerk commented 5 years ago

I ran into the same problem and had to custom code a solution in my transform:

private static string ExtractTypeScriptName(string fullName, bool nullable)
        {
            string compname = nullable ?
                                fullName.TrimEnd('?') :
                                fullName;

            switch (compname)
            {
                case "Boolean":
                case "System.Boolean":
                    return nullable ?
                            "(boolean | null)" :
                            "boolean";
                case "System.String":
                    return nullable ?
                            "(string | null)" :
                            "string";
                case "System.Char":
                case "System.Guid":
                case "System.TimeSpan":
                    return nullable ?
                        "(string | null)" :
                        "string";
                case "System.Byte":
                case "System.SByte":
                case "System.Int16":
                case "System.Int32":
                case "System.Int64":
                case "System.UInt16":
                case "System.UInt32":
                case "System.UInt64":
                case "System.Single":
                case "System.Double":
                case "System.Decimal":
                    return nullable ?
                    "(number | null)" :
                    "number";
                case "System.DateTime":
                case "System.DateTimeOffset":
                case "DateTime":
                case "DateTimeOffset":
                    return nullable ?
                    "(Date | null)" :
                    "Date";
                case "System.Void":
                    return "void";
                case "System.Object":
                case "dynamic":
                    return "any";
            }

            return nullable ?
                    string.Format("({0} | null)", compname) :
                    compname;
        }
GJGowey-biBerk commented 5 years ago

Above function is called via:

    string TypeGen(Property c)
    {
    try {

        if (c.Type?.IsGeneric ?? false)
        {
        return string.Format("({0}[] | null)",ExtractTypeScriptName(c.Type.TypeArguments.FirstOrDefault().FullName, false));
        //c.Type.TypeArguments.FirstOrDefault().FullName);
        }

        if (c.Type.IsValueTuple)
        {
            var vtype = new StringBuilder();
            vtype.Append("{ ");

            foreach (var t in c.Type.TupleElements)
            {

            vtype.AppendFormat("{0} : {1}, ",t.Name, ExtractTypeScriptName(t.Type.FullName, t.Type.IsNullable));
            }
            vtype.Append("}");

            vtype.Replace(", }"," }");
            return vtype.ToString();
        }
        else {        
        return string.Format("{0}",ExtractTypeScriptName(c.Type.FullName, c.Type.IsNullable));
        }
        }catch(Exception e) {
        return string.Format("string // EXCEPTION -- {0}",e);
        }

    }
thechristopher commented 4 years ago

Shorter version which can be used:

  string CustomType(Type t)
  {
    if (t.FullName.EndsWith("?"))
    {
      return "(" + t + " | null)";
    }
    return t;
  }

  //$CustomType inside template
  string CustomType(Parameter p) => CustomType(p.Type);
  string CustomType(Property p) => CustomType(p.Type);
  string CustomType(Method p) => CustomType(p.Type);
  // other contexts can be added