/// <summary> /// Return the TypeScript type (as a string) for specified type. /// </summary> /// <param name="type">IType to query</param> /// <param name="inModelsModule">Pass true if generating the code for the models module, thus model types don't need a "models." prefix</param> /// <returns>TypeScript type string for type</returns> public static string TSType(this IModelType type, bool inModelsModule) { CompositeType composite = type as CompositeType; SequenceType sequence = type as SequenceType; DictionaryType dictionary = type as DictionaryType; PrimaryType primary = type as PrimaryType; EnumType enumType = type as EnumType; string tsType; if (primary != null) { tsType = primary.PrimaryTSType(); } else if (enumType != null) { tsType = "string"; } else if (composite != null) { // ServiceClientCredentials starts with the "msRest." prefix, so strip msRest./msRestAzure. as we import those // types with no module prefix needed var compositeName = composite.Name; if (compositeName.StartsWith("msRest.") || compositeName.StartsWith("msRestAzure.")) { tsType = compositeName.Substring(compositeName.IndexOf('.') + 1); } else if (inModelsModule || compositeName.Contains('.')) { tsType = compositeName; } else { tsType = "models." + compositeName; } } else if (sequence != null) { tsType = sequence.ElementType.TSType(inModelsModule) + "[]"; } else if (dictionary != null) { // TODO: Confirm with Mark exactly what cases for additionalProperties AutoRest intends to handle (what about // additonalProperties combined with explicit properties?) and add support for those if needed to at least match // C# target level of functionality tsType = "{ [propertyName: string]: " + dictionary.ValueType.TSType(inModelsModule) + " }"; } else { throw new NotImplementedException($"Type '{type}' not implemented"); } return(tsType); }
/// <summary> /// Return the TypeScript type (as a string) for specified type. /// </summary> /// <param name="type">IType to query</param> /// <param name="inModelsModule">Pass true if generating the code for the models module, thus model types don't need a "models." prefix</param> /// <returns>TypeScript type string for type</returns> public static string TSType(this IModelType type, bool inModelsModule) { CompositeTypeTS composite = type as CompositeTypeTS; SequenceType sequence = type as SequenceType; DictionaryType dictionary = type as DictionaryType; PrimaryType primary = type as PrimaryType; EnumType enumType = type as EnumType; string tsType; if (primary != null) { tsType = primary.PrimaryTSType(); } else if (enumType != null) { string enumName = enumType.DeclarationName; if (inModelsModule || enumName.Contains('.') || enumName == "string") { tsType = enumName; } else { tsType = "Models." + enumName.ToPascalCase(); } } else if (composite != null) { // ServiceClientCredentials starts with the "msRest." prefix, so strip msRest./msRestAzure. as we import those // types with no module prefix needed var compositeName = composite.UnionTypeName; if (compositeName.StartsWith("msRest.") || compositeName.StartsWith("msRestAzure.")) { tsType = compositeName.Substring(compositeName.IndexOf('.') + 1); } else if (inModelsModule || compositeName.Contains('.')) { tsType = compositeName; } else { tsType = "Models." + compositeName; } } else if (sequence != null) { if (sequence.IsSequenceContainingDateKind()) { tsType = sequence.ElementType.TSType(inModelsModule) + "[]" + " | string[]"; } else { tsType = sequence.ElementType.TSType(inModelsModule) + "[]"; } } else if (dictionary != null) { if (dictionary.IsDictionaryContainingDateKind()) //then provide a union of Date and string { tsType = "{ [propertyName: string]: " + dictionary.ValueType.TSType(inModelsModule) + " }" + " | { [propertyName: string]: string }"; } else { tsType = "{ [propertyName: string]: " + dictionary.ValueType.TSType(inModelsModule) + " }"; } } else { throw new NotImplementedException($"Type '{type}' not implemented"); } return(tsType); }