thoth-org / Thoth.Json

Library for working with JSON in a type safe manner, this libs is targeting Fable
https://thoth-org.github.io/Thoth.Json/
MIT License
150 stars 36 forks source link

Thoth.Json.Core generates invalid TypeScript code #202

Open MangelMaxime opened 5 months ago

MangelMaxime commented 5 months ago

[!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.

Freymaurer commented 5 months ago

Thanks for looking into this!