Skip to content

[TS] invalid TS emited for mutable structs #4453

Closed

Description

@goswinr

When targeting Typescript mutable fields on structs cause the struct to be wraped in FSharpRef<_> , but then the access to the field fails to compile
This happens only when there is mutable after val

[<Struct>]
type R =

    val mutable X: float

    new(x: float) = { X = x }

    member this.IsEmpty() =
        // emits invalid TS
        // Property 'X' does not exist on type 'FSharpRef<Rt>'.
        this.X < 0.0

compiles to

export class R extends Record implements IEquatable<R>, IComparable<R> {
    X: float64;
    constructor(X: float64) {
        super();
        this.X = X;
    }
}

export function R__IsEmpty(this$: FSharpRef<R>): boolean {
    return this$.X < 0; // Property 'X' does not exist on type 'FSharpRef<Rt>'.
}

why is FSharpRef<_> needed here?

Tested on 5.rc5 and in REPL

Activity

  1. goswinr commented on Mar 30, 2026

    @goswinr
    ContributorAuthor

    thanks @ncave for the quick fix,
    I tested 142afe4 .
    this works for members, but still errors on override like this.ToString()

    [<Struct>]
    type R =
    
        val mutable X: float
    
        new(x: float) = { X = x }
    
        override this.ToString() = $"{this.X}" 

    still emits FSharpRef<_>

        toString(): string {
            const this$: FSharpRef<R> = this;
            return `${this$.X}`;
        }
  2. ncave commented on Mar 30, 2026

    @ncave
    Collaborator

    @goswinr Thanks for contributing the issue. Fixed in #4466.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions