NTypewriter icon indicating copy to clipboard operation
NTypewriter copied to clipboard

How to convert .NET classes to TypeScript?

Open washamdev opened this issue 2 years ago • 4 comments

We have some ViewModel classes in our C# code that have properties with types like List<SelectListItem>. How would we get .NET classes like SelectListItem rendered down to TS via NTypewriter, so the TS class generated from this ViewModel knows where to find SelectListItem?

Here is the property in our C# VM:

public List<SelectListItem> CreativeOptions { get; set; } = new List<SelectListItem>();

As it stands right now, NTypewriter generates the following TS for that property:

public CreativeOptions: SelectListItem[];

But of course, that type can't be found because it doesn't exist in our TS yet.

Same question applies to classes from other Nuget packages we install, such as Kendo. If a C# class property is of a type from Kendo's code, how do we get that down to our TS via NTypewriter? Has anyone else dealt with this issue and have any potential solutions?

Thank you!

washamdev avatar Apr 28 '23 14:04 washamdev

I put some examples in this issue: #34

I do exactly this in the examples. The basic premise is understanding that the TS for SelectListItem has already been processed by NTypewriter and exists in a known location. Once you can work with that assumption, then you can recognize that the type of CreativeOptions is a complex type and requires an import for it. You then output the import.

Now, for me, I am never exporting a .Net type in my API. I want more control over my api. I don't want .Net to change the definition of a class on me during an upgrade and then it forces a change to my api. c But given that you are exporting .Net types in your API, I would suspect that data.Classes would contain those classes and you need to find a way to extract them in the for class in data.Classes.

For my builtin classes, I use an attribute on those classes that I want exported, so I can use: for class in data.Classes | Symbols.ThatHaveAttribute "ExportToTypescript"

If you didn't want to hard code the list of classes that need to be exported in your .nt script, you might be able to do two passes over the classes. The first pass finds all the classes you want to export and then creates a list of those classes and the classes that they use as data members. The second pass iterates over data.Classes and matches the class name against the list. If it is found, then export it. This sounds potentially slow, but NTypewriter is already blazingly fast on my fairly large project that maybe the speed will be ok to do two passes.

gregveres avatar Apr 28 '23 14:04 gregveres

I believe that question is mainly about how to get access to types defined outside of our source code, there is an option for that: SearchInReferencedProjectsAndAssemblies

NeVeSpl avatar Apr 28 '23 16:04 NeVeSpl

Thank you both for the helpful responses!

Enabling the SearchInReferencedProjectsAndAssemblies allowed me to now search the Microsoft.AspNetCore.Mvc.Rendering namespace for SelectListItem, so thanks for that!

The remaining problem is that when I include this in my .nt template:

{{~ for dependency in class | Type.AllReferencedTypes ~}}
import {{ dependency.BareName }} = Exported.{{ dependency.FullName }};
{{~ end ~}}

For some reason, it is not detecting that my class is referencing a property that has the type Microsoft.AspNetCore.Mvc.Rendering.SelectListItem. Nothing with the name SelectListItem appears in the imports at all, so the Type.AllReferencedTypes is not finding that property type for some reason. Do you know why this would happen, and how to get it to be found with Type.AllReferencedTypes?

washamdev avatar Apr 28 '23 19:04 washamdev

Because of that line : https://github.com/NeVeSpl/NTypewriter/blob/b2f43fcdc1f22b01bd90c0bc9177a7b7b573dde0/NTypewriter.CodeModel.Functions/TypeFunctions.AllReferencedTypes.cs#L149

This is unfortunately by design, this function does not return types from System.* and Microsoft.* namespaces, and there is no option for changing that (yet).

NeVeSpl avatar Apr 28 '23 20:04 NeVeSpl