glutinum-org / cli

https://glutinum.net/
59 stars 6 forks source link

How to support literal value in type? #111

Open MangelMaxime opened 4 months ago

MangelMaxime commented 4 months ago

Issue created from Glutinum Tool

Glutinum version - 0.9.0-preview

TypeScript

type LiteralBool = { log: true }

FSharp

module rec Glutinum

open Fable.Core
open Fable.Core.JsInterop
open System

[<AllowNullLiteral>]
[<Interface>]
type LiteralBool =
    abstract member log: bool with get, set

Problem description

TypeScript allows to pass literal values in the type but not F#.

To support this syntax, we currently transform the literal value to its type representation. But this means that people could pass false instead of the only accepted value which is true.

Is there something we can do about that?

Perhaps, we could use Emit to generate on the fly the POJO but is it worth the complexity?

module rec Glutinum

open Fable.Core
open Fable.Core.JsInterop
open System

[<Global>]
[<AllowNullLiteral>]
type options
    [<Emit("{name: $0, log: true}")>]
    (
        name : string,
        log: bool
    ) =

    member val log : bool = nativeOnly with get, set

let o = options("maxime", false) // I on purpose used false here to show that the output always use true

generates

export const o = {name: "maxime", log: true};

This is correct but can be confusing for the user.