Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to convert .NET classes to TypeScript? #76

Open
washamdev opened this issue Apr 28, 2023 · 4 comments
Open

How to convert .NET classes to TypeScript? #76

washamdev opened this issue Apr 28, 2023 · 4 comments
Labels

Comments

@washamdev
Copy link

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!

@gregveres
Copy link
Contributor

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.

@NeVeSpl
Copy link
Owner

NeVeSpl commented Apr 28, 2023

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

@washamdev
Copy link
Author

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?

@NeVeSpl
Copy link
Owner

NeVeSpl commented Apr 28, 2023

Because of that line :

if (type.Namespace.StartsWith("System.") || type.Namespace.StartsWith("Microsoft.") ||

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants