-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathTypeFunctions.ToTypeScriptType.cs
133 lines (118 loc) · 4.5 KB
/
TypeFunctions.ToTypeScriptType.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.Linq;
namespace NTypewriter.CodeModel.Functions
{
/// <summary>
/// Set of functions that operates on IType
/// </summary>
public static partial class TypeFunctions
{
/// <summary>
/// Converts type name to typescript type name
/// </summary>
public static string ToTypeScriptType(this IType type, string nullableTypePostfix = "null")
{
if (type == null)
{
return null;
}
var postfix = String.Empty;
if (type.IsNullable && !String.IsNullOrEmpty(nullableTypePostfix))
{
if (!((type is ITypeReferencedByMember typeReference) && (typeReference.Parent?.Attributes.Any(x => x.Name == "Required") == true)))
{
postfix = " | " + (nullableTypePostfix ?? "null");
}
}
return ToTypeScriptTypePhase2(type, nullableTypePostfix) + postfix;
}
private static string ToTypeScriptTypePhase2(IType type, string nullableTypePostfix)
{
if (type.IsArray)
{
// Array : int[]
if (type.ArrayType.IsNullable && !String.IsNullOrEmpty(nullableTypePostfix))
{
return $"({ToTypeScriptType(type.ArrayType, nullableTypePostfix)})[]";
}
return ToTypeScriptType(type.ArrayType, nullableTypePostfix) + "[]";
}
if (type.IsGeneric)
{
var arguments = type.TypeArguments.Select(x => ToTypeScriptType(x, nullableTypePostfix)).ToList();
if (type.IsNullable && type.IsValueType)
{
// nullable value type : int?
return arguments.First();
}
if (type.IsEnumerable)
{
if (arguments.Count() == 1)
{
// List : List<int>
var firstArgument = arguments.First();
if (firstArgument.Contains("|"))
{
return $"({firstArgument})[]";
}
return firstArgument + "[]";
}
if (arguments.Count() == 2)
{
// Dictionary
var keyType = type.TypeArguments.First();
// Dictionary has Enum as a key
if (keyType.IsEnum)
{
return $"{{ [key in keyof typeof {arguments[0]}]?: {arguments[1]} }}";
}
return $"{{ [key: {arguments[0]}]: {arguments[1]} }}";
}
}
// common generic : MyGeneric<int,string>
var name = TranslateNameToTypeScriptName(type);
return $"{name}<{ String.Join(",", arguments) }>";
}
return TranslateNameToTypeScriptName(type);
}
private static string TranslateNameToTypeScriptName(IType type)
{
switch (type.FullName)
{
case "System.Boolean":
return "boolean";
case "System.String":
case "System.Guid":
case "System.Char":
return "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 "number";
case "System.DateTime":
case "System.DateTimeOffset":
return "Date";
case "System.TimeSpan":
return "string";
case "System.Void":
return "void";
case "System.Object":
case "dynamic":
return "any";
}
if ((type.Namespace == "System.Threading.Tasks") && (type.BareName == "Task"))
{
return "Promise";
}
return type.BareName;
}
}
}