ts2fable icon indicating copy to clipboard operation
ts2fable copied to clipboard

EnumCase with Infinity not supported

Open Nexus452 opened this issue 3 years ago • 3 comments

Tried to convert pdf-lib: https://www.npmjs.com/package/pdf-lib ts2fable node_modules/pdf-lib/cjs/index.d.ts pdfLib.fs -e pdf-lib

export declare enum ParseSpeeds { Fastest = Infinity, Fast = 1500, Medium = 500, Slow = 100 }

I locally set: Fastest = 100000 to skip that error

Nexus452 avatar Aug 07 '22 07:08 Nexus452

I don't think we can set an enum with Inifity as a value in F#.

How is Fastest = Infinity represented at runtime by TypeScript?

MangelMaxime avatar Aug 07 '22 09:08 MangelMaxime

I assume it's the same as Number.POSITIVE_INFINITY, so we can represent it in F# as System.Double.PositiveInfinity, though unfortunately it's not supported in enums. This code doesn't compile (literal floats are not supported either):

type ParseSpeeds =    
    | Fastest = System.Double.PositiveInfinity
    | Fast = 1500
    | Medium = 500
    | Slow = 100

I'm thinking now that we added CompiledValue to support TypescriptTaggedUnions when the tag was not a string. In a similar fashion, maybe we could extend Emit support to enums/unions so you could write:

type ParseSpeeds =    
    | [<Emit("Infinity")>] Fastest = 100000000 // Dummy number
    | Fast = 1500
    | Medium = 500
    | Slow = 100

alfonsogarciacaro avatar Aug 08 '22 10:08 alfonsogarciacaro

Thanks for checking, yes that's a good fix.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Infinity

Infinity is a property of the global object. In other words, it is a variable in global scope.

The initial value of Infinity is Number.POSITIVE_INFINITY. The value Infinity (positive infinity) is greater than any other number.

Nexus452 avatar Aug 09 '22 14:08 Nexus452