[TsEnum] not working?

Issue #52 resolved
Nicholas Oxhøj
created an issue

Hi

If I annotate an enum with [TsEnum] like

[TsEnum]
public enum MyEnum
{
    ValueOne,
    ValueTwo
}

no enum is generated in TypeLite.d.ts.

But if I use the enum in a class, the enum is generated in TypeLite.d.ts (to be used in the corresponding interface):

public enum MyEnum
{
    ValueOne,
    ValueTwo
}

[TsClass]
public class MyDummyClass
{
    public MyEnum MyProp { get; set; }
}

Shouldn't I be able to just generate an enum using the [TsEnum] annotation?

Comments (8)

  1. Andre Light

    I agree with Nicholas. There's one key instance where I want an enum available even though I'm not assigning it as any property's type: when I want to use the enum for system/default value reference, while still allowing user values to be assigned to the property.

  2. Andre Light

    The only thing I can think of is to create a class with the TsClass attribute, and then include properties of the type of enums you want generated. If an enum is used in a class as a property type, TypeLite will generate it. That is a small one-time hassle, but until this issue is fixed, it's still much more efficient than manually generating the enums.

  3. Andre Light

    This still seems to be a problem in v1.0.0.0. I recently removed a property that referenced an enum. When I ran the T4 template, the enum was removed from the TypeScript definitions. I ensured that the TsEnum attribute was applied to the public enum (which is not within a class), rebuilt, and tried again with no change in output. I then added the enum-typed property to a class and got the expected output.

    Can anyone else confirm?

  4. Lukas Kabrt repo owner

    I can't reproduce the problem you have. I added a new enum to the Demo project annotated with the [TsEnum] attribute, built the project, regenerated .d.ts files and the enum appeared in the enum.ts file. I used .T4 code from the Demo project:

    <#@ template debug="false" hostspecific="True" language="C#" #>
    
    <#@ assembly name="$(TargetDir)TypeLite.dll" #>
    <#@ assembly name="$(TargetDir)TypeLite.Net4.dll" #>
    <#@ assembly name="$(TargetDir)$(TargetFileName)" #>
    <#@ assembly name="$(TargetDir)TypeLite.Demo.Portable.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()
            .ForLoadedAssemblies()
            .WithReference("Enums.ts")
            .WithIndentation("  ");
    #>
    
    <#= ts.Generate(TsGeneratorOutput.Properties) #>
    
    <# manager.StartNewFile("Enums.ts"); #>
    <#= ts.Generate(TsGeneratorOutput.Enums) #>
    <# manager.EndBlock(); #>
    <# manager.Process(true); #>
    
  5. Andre Light

    Lukas, thanks for the response.

    I was able to verify that it works for enums that are within the project in which TypeLite runs. However, I store the majority of enums in a separate project because multiple projects reference those enums--I had to do that to avoid circular project references. I could run TypeLite in the global Enums project and then reference that file in another project's _references.ts, but then I would have to exclude the assembly from building in the current TypeLite project to avoid duplicate definitions; I don't see a way to do that easily in your current documentation, and I have too many class objects to add each object that should be included in the T4 file (currently, I use TypeLite attributes for that).

    Is it possible to get this to work for referenced projects, as well?

    Here's my T4 code, which is almost the same as yours:

    <#@ template debug="false" hostspecific="True" language="C#" #>
    <#@ assembly name="$(TargetDir)TypeLite.dll" #>
    <#@ assembly name="$(TargetDir)TypeLite.Net4.dll" #>
    <#@ assembly name="$(TargetDir)$(TargetFileName)" #>
    
    <#@ 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()
                    .ForLoadedAssemblies()
                    .WithReference("TypeLite.GlobalRef.Enums.ts")
                    .WithFormatter((type, f) => "I" + ((TypeLite.TsModels.TsClass)type).Name);
    #>
    
    <#= ts.Generate(TsGeneratorOutput.Properties) #>
    
    <# manager.StartNewFile("TypeLite.GlobalRef.Enums.ts"); #>
    <#= ts.Generate(TsGeneratorOutput.Enums) #>
    
    <# manager.EndBlock(); #>
    <# manager.Process(true); #>
    

    Thanks again.

  6. Log in to comment