Typelite POCO Class Generation
Asked Answered
W

4

12

Is it possible for Typelite to generate a TypeScript class instead of an interface? Something like:

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

to

export class Person
{
    constructor() {}
    FirstName: string;
    LastName: string;
}

I'm not looking for any functionality in the generated classes, only an easier way to be able to instantiate classes clientside without having to initialize the entire interface.

For example, I would prefer being able to do this:

var person = new Person();

Instead of

var person = {
    FirstName: null,
    LastName: null
};
Waterfall answered 7/10, 2014 at 14:30 Comment(0)
P
9

What I did was make a simple adjustment to the tt file, saves you from compiling your own typelite:

<#= definitions.ToString()
.Replace("interface", "export class")
.Replace("declare module", "module") #>
Palaeolithic answered 30/10, 2014 at 13:54 Comment(2)
Thanks. That is helpful. It does mean I will need to generate 2 files since I am only interested in this happening on specific classes.Waterfall
Where do I make this change? Can you share your TypeLite.Net4.tt classImpermeable
A
5

This feature is supported by Reinforced.Typings.

Using attribute

[TsClass]
public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

or fluent call

public class Configuration
{
    public static void Configure(ConfigurationBuilder builder) 
    {
            builder
                .ExportAsClass<Person>()
                .WithPublicProperties();
    }
}

will produce following to the output file:

namespace MyApp {
    export class User
    {
        public FirstName: string;
        public LastName : string;
    }
}
Always answered 16/3, 2016 at 5:58 Comment(0)
P
4

Unfortunately this scenario isn't supported right now.

I have never missed such feature, because classes usually have some methods, so it didn't make a sense to me. But feel free to fiddle with the source ('TsGenerator.cs'), it shouldn't be too difficult to generate classes instead of interfaces if you don't require any methods in the classes.

Posh answered 7/10, 2014 at 20:59 Comment(7)
Thanks. For me it makes sense to generate a class, where all properties have private setters and are initialized from constructor. In this way it will be easier to escape initialize errors on client side. But not sure if it's ever possible.Counterclockwise
It is certainly possible, but the classes can't have any behaviour (like methods) because every time you run TypeLite your custom code get lost. If TypeScript has support for something like partial classes I will add support for generating classes, but I'm not aware of such concept TypeScript.Posh
For me there's no any need in methods since in my opinion it would be a bad practice to add behavior to DTOs. Just a class with ctorCounterclockwise
My use case is simply for DTOs with no behavior. I literally just want to instantiate a class that satisfies the interface without having to recreate what Typelite already does so nicely for me. I would fully expect to have to attach behavior or values through some other means.Waterfall
This sounds like a common request. Is there a place to make such feature requests to litesolutions.net?Impermeable
bitbucket.org/LukasKabrt/typelite/… ... feature requests with pull requests are preferred ;-)Posh
FYI for others this feature request is @ bitbucket.org/LukasKabrt/typelite/issues/66/…Impermeable
E
0

Expanding on @Flores' answer above, in the latest version of TypeLite (1.8.4 as of right now), you can modify the call to ts.Generate(...) as follows:

<#= ts.Generate(TsGeneratorOutput.Properties)
    .Replace("interface", "export class")
    .Replace("declare module", "module") #>

Full Transform:

<#@ template debug="false" hostspecific="True" language="C#" #>
<#@ assembly name="$(TargetDir)TypeLite.dll" #>
<#@ assembly name="$(TargetDir)TypeLite.Net4.dll" #>
<#@ assembly name="$(TargetDir)MyAssembly.dll" #>

<#@ import namespace="TypeLite" #> 
<#@ import namespace="TypeLite.Net4" #> 
<#@output extension=".d.ts"#>

<#@include file="Manager.ttinclude"#>
<# var manager = Manager.Create(Host, GenerationEnvironment); #>

<# var ts = TypeScript.Definitions()
    .WithReference("Enums.ts")
    .ForLoadedAssemblies();
#>

<#= ts.Generate(TsGeneratorOutput.Properties)
    .Replace("interface", "export class")
    .Replace("declare module", "module") #>

<# manager.StartNewFile("Enums.ts"); #>
<#= ts.Generate(TsGeneratorOutput.Enums) #>
<# manager.EndBlock(); #>
<# manager.Process(true); #>
Erosion answered 9/5, 2017 at 13:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.