@@ -26,7 +26,10 @@ public TypeScriptGenerator(MetadataTypesConfig config)
2626 { "String" , "string" } ,
2727 { "Boolean" , "boolean" } ,
2828 { "DateTime" , "string" } ,
29+ { "DateTimeOffset" , "string" } ,
2930 { "TimeSpan" , "string" } ,
31+ { "Guid" , "string" } ,
32+ { "Char" , "string" } ,
3033 { "Byte" , "number" } ,
3134 { "Int16" , "number" } ,
3235 { "Int32" , "number" } ,
@@ -67,6 +70,7 @@ public string GetCode(MetadataTypes metadata, IRequest request, INativeTypesMeta
6770 sb . AppendLine ( "BaseUrl: {0}" . Fmt ( Config . BaseUrl ) ) ;
6871 sb . AppendLine ( ) ;
6972 sb . AppendLine ( "{0}GlobalNamespace: {1}" . Fmt ( defaultValue ( "GlobalNamespace" ) , Config . GlobalNamespace ) ) ;
73+ sb . AppendLine ( "{0}ExportAsTypes: {1}" . Fmt ( defaultValue ( "ExportAsTypes" ) , Config . ExportAsTypes ) ) ;
7074 sb . AppendLine ( "{0}MakePropertiesOptional: {1}" . Fmt ( defaultValue ( "MakePropertiesOptional" ) , Config . MakePropertiesOptional ) ) ;
7175 sb . AppendLine ( "{0}AddServiceStackTypes: {1}" . Fmt ( defaultValue ( "AddServiceStackTypes" ) , Config . AddServiceStackTypes ) ) ;
7276 sb . AppendLine ( "{0}AddResponseStatus: {1}" . Fmt ( defaultValue ( "AddResponseStatus" ) , Config . AddResponseStatus ) ) ;
@@ -196,7 +200,11 @@ private string AppendType(ref StringBuilderWrapper sb, MetadataType type, string
196200
197201 if ( type . IsEnum . GetValueOrDefault ( ) )
198202 {
199- sb . AppendLine ( "enum {0}" . Fmt ( Type ( type . Name , type . GenericArgs ) ) ) ;
203+ var typeDeclaration = ! Config . ExportAsTypes
204+ ? "enum"
205+ : "export const enum" ;
206+
207+ sb . AppendLine ( "{0} {1}" . Fmt ( typeDeclaration , Type ( type . Name , type . GenericArgs ) ) ) ;
200208 sb . AppendLine ( "{" ) ;
201209 sb = sb . Indent ( ) ;
202210
@@ -223,26 +231,36 @@ private string AppendType(ref StringBuilderWrapper sb, MetadataType type, string
223231 if ( type . Inherits != null )
224232 extends . Add ( Type ( type . Inherits ) . InheritedType ( ) ) ;
225233
234+ var interfaces = new List < string > ( ) ;
226235 if ( options . ImplementsFn != null )
227236 {
228237 var implStr = options . ImplementsFn ( ) ;
229238 if ( ! string . IsNullOrEmpty ( implStr ) )
230- extends . Add ( implStr ) ;
239+ interfaces . Add ( implStr ) ;
231240 }
232241
242+ var isClass = Config . ExportAsTypes && ! type . IsInterface . GetValueOrDefault ( ) ;
233243 var extend = extends . Count > 0
234- ? " extends " + ( string . Join ( ", " , extends . ToArray ( ) ) )
244+ ? " extends " + extends [ 0 ]
235245 : "" ;
236246
237- sb . AppendLine ( "interface {0}{1}" . Fmt ( Type ( type . Name , type . GenericArgs ) , extend ) ) ;
247+ if ( interfaces . Count > 0 )
248+ extend += " implements " + string . Join ( ", " , interfaces . ToArray ( ) ) ;
249+
250+ var typeDeclaration = ! Config . ExportAsTypes
251+ ? "interface"
252+ : "export {0}" . Fmt ( isClass ? "class" : "interface" ) ;
253+
254+ sb . AppendLine ( "{0} {1}{2}" . Fmt ( typeDeclaration , Type ( type . Name , type . GenericArgs ) , extend ) ) ;
238255 sb . AppendLine ( "{" ) ;
239256
240257 sb = sb . Indent ( ) ;
241258
242259 var addVersionInfo = Config . AddImplicitVersion != null && options . IsRequest ;
243260 if ( addVersionInfo )
244261 {
245- sb . AppendLine ( "{0}?: number; //{1}" . Fmt ( "Version" . PropertyStyle ( ) , Config . AddImplicitVersion ) ) ;
262+ sb . AppendLine ( "{0}{1}: number; //{2}" . Fmt (
263+ "Version" . PropertyStyle ( ) , isClass ? "" : "?" , Config . AddImplicitVersion ) ) ;
246264 }
247265
248266 AddProperties ( sb , type ,
@@ -276,15 +294,15 @@ public void AddProperties(StringBuilderWrapper sb, MetadataType type, bool inclu
276294 propType = propType . Substring ( 0 , propType . Length - 1 ) ;
277295 optional = "?" ;
278296 }
297+
279298 if ( Config . MakePropertiesOptional )
280- {
281299 optional = "?" ;
282- }
283300
284301 if ( prop . Attributes . Safe ( ) . FirstOrDefault ( x => x . Name == "Required" ) != null )
285- {
286302 optional = "" ;
287- }
303+
304+ if ( Config . ExportAsTypes && ! type . IsInterface . GetValueOrDefault ( ) )
305+ optional = "" ;
288306
289307 wasAdded = AppendDataMember ( sb , prop . DataMember , dataMemberIndex ++ ) ;
290308 wasAdded = AppendAttributes ( sb , prop . Attributes ) || wasAdded ;
@@ -297,7 +315,8 @@ public void AddProperties(StringBuilderWrapper sb, MetadataType type, bool inclu
297315 if ( wasAdded ) sb . AppendLine ( ) ;
298316
299317 AppendDataMember ( sb , null , dataMemberIndex ++ ) ;
300- sb . AppendLine ( "{0}?: ResponseStatus;" . Fmt ( typeof ( ResponseStatus ) . Name . PropertyStyle ( ) ) ) ;
318+ sb . AppendLine ( "{0}{1}: ResponseStatus;" . Fmt (
319+ typeof ( ResponseStatus ) . Name . PropertyStyle ( ) , Config . ExportAsTypes ? "" : "?" ) ) ;
301320 }
302321 }
303322
0 commit comments