Open johnknoop opened 5 years ago
This is my C# class that is being generated using $Classes:
public class Species { public string Name { get; set; } public IList<TreeItem<string>> SubspeciesNames { get; set; } }
I use this template to generate TS interfaces:
export interface $Name$TypeParameters {$Properties[ $Prop;] }]
which results in a Species.ts that looks like this:
export interface Species { name: string; subspeciesNames: TreeItem<string>[]; }
So far so good. But now I want to also include the referenced type TreeItem in the same output file.
The TreeItem class looks like this:
public class TreeItem<T> { public T Item { get; set; } public IEnumerable<TreeItem<T>> Children { get; set; } }
In order to do that, I've created a function called ReferencedTypes like this:
ReferencedTypes
IEnumerable<Type> ReferencedTypes(File f) { bool IsDefinedInFile(Type t) { return f.Classes.Any(x => x.FullName == t.FullName) || f.Interfaces.Any(x => x.FullName == t.FullName); } Type GetImportableType(Type t) { if (t.IsEnumerable && t.TypeArguments.Any()) { return GetImportableType(t.TypeArguments.First()); } return !IsDefinedInFile(t) && t.IsDefined && !t.IsEnum && t.Namespace.StartsWith("MyNamespace") ? t : null; } var allClassesInFile = f.Classes.Concat(f.Classes.SelectMany(c => c.NestedClasses)); return allClassesInFile.SelectMany(x => x.Properties.Select(p => GetImportableType(p.Type)).Where(t => t != null)); }
And I'm calling it like this:
$ReferencedTypes[ export interface $OriginalName$TypeParameters $BaseClass[extends $Name]{ $Properties[ $name: $Type[$Unwrap[$OriginalName]$TypeParameters];] } ]
The expected output of $ReferencedTypes would be:
export interface TreeItem<T> { item: T; children: TreeItem<T>; }
But the actual output is this:
export interface TreeItem<T> { item: string; children: TreeItem<T>; }
Notice how the type of the item property is string and not T. I suppose this is because the Species class is passing string as type argument, but that's not what I'm telling it to render.
item
string
T
Is this a bug, or am I doing it wrong?
This is my C# class that is being generated using $Classes:
I use this template to generate TS interfaces:
which results in a Species.ts that looks like this:
So far so good. But now I want to also include the referenced type TreeItem in the same output file.
The TreeItem class looks like this:
In order to do that, I've created a function called
ReferencedTypes
like this:And I'm calling it like this:
The expected output of $ReferencedTypes would be:
But the actual output is this:
Notice how the type of the
item
property isstring
and notT
. I suppose this is because the Species class is passingstring
as type argument, but that's not what I'm telling it to render.Is this a bug, or am I doing it wrong?