Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JS] JsInterop.jsOptions produces unexpected output for nested types #4010

Open
Linschlager opened this issue Jan 14, 2025 · 2 comments
Open

Comments

@Linschlager
Copy link

Description

JsInterop.jsOptions compiles to a js record containing the specified fields. This works nicely for simple interfaces, however it behaves unexpectedly with nested ones.

REPL

Repro code

open Fable.Core

[<AllowNullLiteral>]
type MyInnerType =
  abstract member value: int with get, set

[<AllowNullLiteral>]
type MyOuterType =
  abstract member nested: MyInnerType with get, set

let x = JsInterop.jsOptions<MyOuterType> (fun o ->
  o.nested.value <- 10
)

Actual output

export const x = {
    value: 10,
};

Expected output

export const x = {
    nested = {
        value: 10,
    }
};

or compilation error

Related information

  • REPL: 4.2.0
  • Fable: 4.23.0
@MangelMaxime
Copy link
Member

I think you are supposed to call jsOptions again for the inner type:

open Fable.Core
open Fable.Core.JsInterop

[<AllowNullLiteral>]
type MyInnerType =
  abstract member value: int with get, set

[<AllowNullLiteral>]
type MyOuterType =
  abstract member nested: MyInnerType with get, set

let x = jsOptions<MyOuterType> (fun o ->
  o.nested <- jsOptions<MyInnerType>(fun o ->
    o.value <- 10
  )
)
export const x = {
    nested: {
        value: 10,
    },
};

Perhaps, it is possible to make jsOptions works the way you shown but this will need to be investigated as I am not familiar with that part of the code yet.

@Linschlager
Copy link
Author

@MangelMaxime Yes, that was my conclusion as well and that works nicely, however it is a bit more verbose.
It mostly seemed like unexpected behaviour which is why I reported it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants