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

Type.ToTypeScriptType Generating Incorrectly #38

Closed
vcastellano-spl opened this issue Sep 27, 2021 · 7 comments
Closed

Type.ToTypeScriptType Generating Incorrectly #38

vcastellano-spl opened this issue Sep 27, 2021 · 7 comments
Assignees
Labels
invalid This doesn't seem right wontfix This will not be worked on

Comments

@vcastellano-spl
Copy link

vcastellano-spl commented Sep 27, 2021

Installed from Visual Studio "Manage Extensions", version 0.1.11

Template:

{{ config.NamespacesToBeSearched = ["SNAP.DAL.Models"] }}

{{- # Helper classes }}

{{- func ImportType(type)
	ret "import { " | String.Append type | String.Append " } from './" | String.Append type | String.Append "'\r"
	end
}}

{{- func TypeName(type)
		useType = Type.Unwrap(type)
		if (useType.ArrayType != null) 
			useType = useType.ArrayType
		end
		ret useType.Name
	end
}}

{{- # output classes }}
{{- for class in data.Classes
		capture output }}
{{- for type in (class | Type.AllReferencedTypes | Array.Each @TypeName | Array.Uniq )}}
	{{- ImportType type}}
{{-end}}
export interface {{class.Name}}{{if class.HasBaseClass}} extends {{class.BaseClass.Name; end}} {
  {{-for prop in class.Properties | Symbols.ThatArePublic }}
  {{ prop.Name }}: {{prop.Type | Type.ToTypeScriptType}}{{if !for.last}},{{end}}
  {{-end}}
}
{{end}}
{{- Save output ("../../../snapgui/src/models/" + class.BareName + ".ts")}}
{{- end}}

C# Model:

namespace SNAP.DAL.Models
{
    public partial class User
    {

        [Key]
        public int UserId { get; set; }
        [Required]
        [StringLength(50)]
        public string Username { get; set; }
        [Required]
        public string Password { get; set; }
        public int RoleMask { get; set; }

        public int? SupervisorId { get; set; }

        public virtual List<Client> ClientDefaultSupervisors { get; } = new List<Client>();
        public virtual List<Client> ClientDefaultStaff { get; } = new List<Client>();
        public virtual List<Review> ReviewStaff { get; } = new List<Review>();
        public virtual List<Review> ReviewSupervisors { get; } = new List<Review>();
    }
}

Generated Typescript:

import { Client } from './Client'
import { Review } from './Review'

export interface User {
  UserId: number,
  Username: string,
  Password: string,
  RoleMask: number,
  SupervisorId: int<number> | null,
  ClientDefaultSupervisors: List<Client>,
  ClientDefaultStaff: List<Client>,
  ReviewStaff: List<Review>,
  ReviewSupervisors: List<Review>
}

Expected Typescript:

import { Client } from './Client'
import { Review } from './Review'

export interface User {
  UserId: number,
  Username: string,
  Password: string,
  RoleMask: number,
  SupervisorId: number | null,
  ClientDefaultSupervisors: Client[],
  ClientDefaultStaff: Client[],
  ReviewStaff: Review[],
  ReviewSupervisors: Review[]
}

The issues seem to be with any List<> in C#, and any nullable ints, it gets the nullable part right, but I can't see why it's generating the invalid int type.

@vcastellano-spl
Copy link
Author

DateTime? also seems to be generating DateTime<DateTime> | null and importing DateTime as a custom type.

@NeVeSpl NeVeSpl added the bug Something isn't working label Sep 27, 2021
@NeVeSpl
Copy link
Owner

NeVeSpl commented Sep 27, 2021

Something weird is going on, there are unit tests for those cases, and they are passing with flying colours. Could you prepare a simple minimal project that exposes problems and upload it on github? Also what version of VS are you using?

@NeVeSpl NeVeSpl self-assigned this Sep 27, 2021
@vcastellano-spl
Copy link
Author

Simple, minimal project works as expected. Using latest VS 2019 updates.

I'll see if I can debug the plugin itself (not familiar with the process, but I'll take a look), to see what about this project would cause it. I m using EFCore 6, and these are models for that, but even after removing EF attributes they are rendering like that.

@vcastellano-spl
Copy link
Author

OK, I did not figure out how to debug it, however I tried with the https://github.com/TietoEVRY/NTypewriterCLI CLI interface, and that worked correctly, when pointing at the project file.

But that lead me to review the project in more detail, and I realized this is VS2019, but the project is itself the RC build of .NET6. If I change the TargetFramework to net5.0, it works. So this is on me for using RC build. But it may be worth identifying the cause, as it is RC now but it will be launching in a little over a month!

I can try and dig a little more when/if I get some time, but feel free to close this if you don't want to resolve for .NET6 yet.

@NeVeSpl
Copy link
Owner

NeVeSpl commented Sep 28, 2021

How did you manage to build/compile .net 6.0 project with VS 2019? As far as I know, VS 2019 does not support .net 6.0 projects, you can open them, but you will not be able to build/compile...

@Etchelon
Copy link
Contributor

@vcastellano-spl congrats for diving into the problem and helping out the maintainer of the project, not something many people do!
Also maybe it's a typo, but you should use ; instead of , in the TS interface definition to separate the properties.

@vcastellano-spl
Copy link
Author

@NeVeSpl Long story, but this was because I was developing on two different machines, and the C# code was on a machine I was using VS2022 Preview on, and the frontend code was being developed on a different machine, and I was just trying to get the models to be available (but not compiling or running). Anyways, I am sorry for the confusion, I should have known it would be something like this. We can close this out as there's already a VS2022.

@Etchelon I should move it over, I'm newer to TS, and used the example for the template in the comments #34, which uses commas (which seem to be supported since TS1.6?) but agreed best practices show as ;.

@NeVeSpl NeVeSpl added invalid This doesn't seem right wontfix This will not be worked on and removed bug Something isn't working labels Sep 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants