Decimal constants are not generated to TypeScript

Create issue
Issue #133 new
Former user created an issue

Hello, currently we are generating C# constants to TypeScript. The constants are public const string and public const decimal. However, only string constants are generated to TypeScript. I even upgraded to latest TypeLite to see if the issue was resolved, but it occurs also in latest version.

Comments (3)

  1. Alex Muirhead

    Hi,

    The problem is that decimal has no direct primitive type in JavaScript.

    You can set it to string using the WithConvertor method, for the fluent interface it would look like this:

        .WithConvertor<decimal>(t => "string")
    

    I'd also recommend using BigJS or similar.

  2. Mek

    Thank you, but it doesn't look like it's working.

    <#var tsConstants = 
        TypeScript.Definitions()
            .For(typeof(blahblahblah)).ToModule(defaultNamespace)
            .For<blahblahblah2>().ToModule(defaultNamespace).WithConvertor<decimal>(t => "number")
            .For<blahblahblah3>().ToModule(defaultNamespace);#>
    <# manager.StartNewFile("constants.ts"); #>
    <#=tsConstants.Generate(TsGeneratorOutput.Constants)#>
    

    I tried to place the .WithConvertor bit of code into various places but I either got error that TypeScriptFluent does not have method ToModule() or it did nothing. I would expect my constant pop up in typescript file with type number. Anyway, now I tried it with "string" instead of "number" and the constants were generated like this:

            export const PercentageMinValue: string = -100000000000000;
            export const PercentageMaxValue: string = 100000000000000;
    

    This is not correct and TypeScript complains that type number is not assignable to type string.

  3. Alex Muirhead

    Hmm, I can't think of a cleaner way, other than to implement your own TsGenerator class.

    That way you can override AppendConstantModule method.

    The problem is that GetPropertyConstantValue is not virtual, maybe you could submit a pull request that makes it so. Anyway, in the overridden AppentConstantModule method, you can call your own method for GetPropertyConstantValue and if the type is decimal, convert it to string.

    You can find the methods here: https://bitbucket.org/LukasKabrt/typelite/src/076249af9d12d08315cab8b4f1531716d46c78e4/TypeLite/TsGenerator.cs?at=default&fileviewer=file-view-default

  4. Log in to comment