Skip to content

Adding generic static member in a class emits invalid TypeScript #3504

@OneManEquipe

Description

@OneManEquipe

Description

If I define a generic class with a static member, Fable emits invalid TypeScript code (does not compile).

Repro code

Here is a playground link

And here is the same code:

open Fable.Core

[<AttachMembers>]
type Counter<'TRaw> () =
    static member CheckLength(xs: 'TRaw list) =
        xs.Length

Expected and actual results

The result that I would expect is valid TypeScript code, but what I get it this:

import { FSharpList, length } from "fable-library/List.js";
import { int32 } from "fable-library/Int32.js";
import { class_type, TypeInfo } from "fable-library/Reflection.js";

export class Counter$1<TRaw> {
    constructor() {
    }
    static CheckLength(xs: FSharpList<TRaw>): int32 {
        return length(xs);
    }
}

export function Counter$1_$reflection(gen0: TypeInfo): TypeInfo {
    return class_type("Test.Counter`1", [gen0], Counter$1);
}

export function Counter$1_$ctor<TRaw>(): Counter$1<TRaw> {
    return new Counter$1();
}

Pasting the code in the TypeScript playground I get the following error:

Static members cannot reference class type parameters.
Parameter 'xs' of public static method from exported class has or is using private name 'TRaw'.
'TRaw' is declared but its value is never read.

The culprit is the following method:

static CheckLength(xs: FSharpList<TRaw>): int32 {
    return length(xs);
}

I think the correct version should be:

static CheckLength<TRaw>(xs: FSharpList<TRaw>): int32 {
    return length(xs);
}

which actually fixes the problem (but I'm not an expert on TS, I'm not sure if that is the best solution).

Related information

  • Fable version: 4.1.3 (playground) and 4.1.4 (local)
  • Operating system: Mac OS 13.4

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions