Skip to content

Commit 11a5c29

Browse files
committed
fix: use Record type for typescript dictionaries
fix: typescript dictionary with generic value type
1 parent d75173b commit 11a5c29

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed
Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
1-
using System.Linq;
2-
using KY.Core;
1+
using KY.Core;
32
using KY.Generator.Output;
43
using KY.Generator.Templates;
54
using KY.Generator.Writers;
65

7-
namespace KY.Generator.TypeScript.Writers
6+
namespace KY.Generator.TypeScript.Writers;
7+
8+
public class TypeScriptDictionaryWriter : IGenericTypeWriter
89
{
9-
public class TypeScriptDictionaryWriter : IGenericTypeWriter
10+
public void Write(GenericTypeTemplate template, IOutputCache output)
1011
{
11-
public void Write(GenericTypeTemplate template, IOutputCache output)
12+
TypeTemplate keyType = template.Types.First();
13+
TypeTemplate valueType = template.Types.Second();
14+
if (keyType.Name is "string" or "number")
15+
{
16+
output.Add("Record<").Add(keyType).Add(", ").Add(valueType).Add(">");
17+
}
18+
else
1219
{
13-
TypeTemplate keyType = template.Types.First();
14-
TypeTemplate valueType = template.Types.Second();
15-
if (keyType.Name is "string" or "number")
16-
{
17-
output.Add($"{{ [key: {keyType.Name}]: {valueType.Name}; }}");
18-
}
19-
else
20-
{
21-
output.Add($"{{ /* unsupported type for key. Expected string or number. Got '{keyType.Name}'. */ }}");
22-
}
20+
output.Add($"{{ /* unsupported type for key. Expected string or number. Got '{keyType.Name}'. */ }}");
2321
}
2422
}
2523
}

0 commit comments

Comments
 (0)