Skip to content

Commit

Permalink
Add support for dictionary typed properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexperovich committed May 18, 2015
1 parent 9793bf4 commit f5e1362
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CS2TS.Tests/DerivedTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class Test2 : Dictionary<string, {0}>
public class Test3 : Test2
{{
public {0} Prop {{ get; set; }}
public Dictionary<string, {0}> Dict {{ get; set; }}
}}
}}
", cSharpType);
Expand All @@ -54,6 +55,7 @@ public class Test3 : Test2
{2} interface Test3 extends Test2 {{
Prop{1}: {0};
Dict?: {{ [key: string]: {0}; }};
}}
", typescriptType, cSharpType.ToLower().Contains("string") ? "?" : "", generateDeclarations ? "declare" : "export"), output);
Expand Down
25 changes: 19 additions & 6 deletions CS2TS/TypeScriptMemberEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,20 @@ private string GetPropertyName(IPropertySymbol propertySymbol)

private string GetTypescriptType(ITypeSymbol type)
{
if (type.MetadataName == "IDictionary`2")
{
var dictionaryType = (INamedTypeSymbol) type;
var keyType = dictionaryType.TypeArguments[0];
var valueType = dictionaryType.TypeArguments[1];
if (keyType.SpecialType == SpecialType.System_String)
{
return string.Format("{{ [key: string]: {0}; }}", GetTypescriptType(valueType));
}
if (IsNumber(keyType))
{
return string.Format("{{ [key: number]: {0}; }}", GetTypescriptType(valueType));
}
}
if (type.TypeKind == TypeKind.Array)
{
IArrayTypeSymbol arr = (IArrayTypeSymbol) type;
Expand Down Expand Up @@ -190,13 +204,12 @@ private string GetTypescriptType(ITypeSymbol type)
case SpecialType.System_Collections_Generic_IReadOnlyList_T:
var namedType = (INamedTypeSymbol) type;
return GetTypescriptType(namedType.TypeArguments.First()) + "[]";
default:
if (_semanticModel.Compilation.Assembly.TypeNames.Any(tn => type.Name == tn))
{
return type.Name;
}
return type.AllInterfaces.Select(GetTypescriptType).FirstOrDefault(t => t != "any") ?? "any";
}
if (_semanticModel.Compilation.Assembly.TypeNames.Any(tn => type.Name == tn))
{
return type.Name;
}
return type.AllInterfaces.Select(GetTypescriptType).FirstOrDefault(t => t != "any") ?? "any";
}

private static bool IsNullable(ITypeSymbol type)
Expand Down

0 comments on commit f5e1362

Please sign in to comment.