Thoth.Json icon indicating copy to clipboard operation
Thoth.Json copied to clipboard

Thoth.Json.Core generates invalid TypeScript code

Open MangelMaxime opened this issue 8 months ago • 1 comments

[!NOTE] This is reproduction code

type IDecoderHelpers<'JsonValue> =
    abstract isString: 'JsonValue -> bool
    abstract asString: 'JsonValue -> string

type Decoder<'T> =
    abstract member Decode<'JsonValue> :
        helpers: IDecoderHelpers<'JsonValue> * value: 'JsonValue -> obj

let invalidGenerics: Decoder<string> =
        { new Decoder<string> with
            member _.Decode(helpers, value) =
                null
        }

let correctGenerics: Decoder<string> =
        { new Decoder<string> with
            member _.Decode<'JsonValue>(helpers : IDecoderHelpers<'JsonValue>, value) =
                null
        }
import { defaultOf } from "fable-library-js/Util.js";

export interface IDecoderHelpers$1<JsonValue> {
    asString(arg0: JsonValue): string,
    isString(arg0: JsonValue): boolean
}

export interface Decoder$1<T> {
    Decode<JsonValue>(helpers: IDecoderHelpers$1<JsonValue>, value: JsonValue): any
}

export const invalidGenerics: Decoder$1<string> = {
    Decode<JsonValue>(helpers: IDecoderHelpers$1<$a>, value: $a): any {
        return defaultOf();
    },
};

export const correctGenerics: Decoder$1<string> = {
    Decode<JsonValue>(helpers: IDecoderHelpers$1<JsonValue>, value: JsonValue): any {
        return defaultOf();
    },
};

See how the invalidGenerics is using $a instead of JsonValue.

This problem was first reported on Fable repository https://github.com/fable-compiler/Fable/issues/3586 and in private to me.

We should fix this in Fable, but to workaround this issue it is possible to help the compiler infer the correct generics name. In order, to catch all occurence of this issues and make sure Thoth.Json.Core and Thoth.Json.JavaScript can be used on TypeScript, we should also run the tests against TypeScript.

MangelMaxime avatar Jun 20 '24 08:06 MangelMaxime