Skip to content

Explicit base constructor call not properly called #2425

Description

@ncave
type Node =
    val X: int
    new (x: int) = { X = x }

type Leaf =
    inherit Node
    val Y: string
    new (x: int, y: string) =
        { inherit Node(x); Y = y }

[<EntryPoint>]
let main _args =
    let node = Leaf(3, "4")
    let x = node.X  // <-- Error: This should be 3, but is undefined.
    let y = node.Y
    System.Console.WriteLine($"x: {x}, y: {y}")
    0

Activity

  1. github-actions commented on Mar 19, 2026

    @github-actions
    Contributor

    🤖 This is an automated response from Repo Assist.

    Thanks for filing this, @ncave! This is a known gap in Fable's class hierarchy support.

    Root cause: Fable has limited support for F#'s explicit val field syntax combined with the record-constructor syntax ({ inherit Node(x); Y = y }). In the generated JavaScript, the base constructor call (inherit Node(x)) doesn't result in super(x) being invoked properly, so Node's field initialisation never runs and X remains undefined.

    Workaround: Use the standard F# primary-constructor syntax, which Fable handles reliably:

    type Node(x: int) =
        member val X = x
    
    type Leaf(x: int, y: string) =
        inherit Node(x)
        member val Y = y
    
    [(EntryPoint)]
    let main _args =
        let node = Leaf(3, "4")
        let x = node.X  // correctly returns 3
        let y = node.Y
        System.Console.WriteLine($"x: {x}, y: {y}")
        0

    This pattern maps cleanly to ES6 classes with super(x) and getter properties.

    Where to fix: The issue lives in the FSharp2Fable or Fable2Babel stage — when a class uses val fields with new () = { inherit Base(args); ... }, the base constructor call needs to be translated into a super(...) call in the generated JavaScript class constructor.

    Generated by Repo Assist · ◷

    To install this agentic workflow, run

    gh aw add githubnext/agentics/workflows/repo-assist.md@346204513ecfa08b81566450d7d599556807389f
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions