ts2fable icon indicating copy to clipboard operation
ts2fable copied to clipboard

Generated tests for erased unions not working with Fable 2

Open alfonsogarciacaro opened this issue 7 years ago • 5 comments

People are reporting that the automatically generated tests for erased unions are not compiling in Fable 2. See: https://github.com/fable-compiler/Fable/issues/1580#issuecomment-428425911

For example:

type VerifyErrors =
    U3<JsonWebTokenError, NotBeforeError, TokenExpiredError>

module VerifyErrors =
   let isJsonWebTokenError (v: VerifyErrors) = match v with U3.Case1 _ -> true | _ -> false

The problem is not the erased union, but JsonWebTokenError. ts2fable generates types as interfaces and Fable 2 is throwing a compile error if trying to type test against an interface because it doesn't know how to identify the interface. Fable 1 added the interface name to types coming from F#, but this didn't work with JS objects, so we've opted to be more strict in Fable 2 and avoid unexpected behaviour at runtime.

Should we stop generating these automatic matches?

alfonsogarciacaro avatar Oct 10 '18 08:10 alfonsogarciacaro

Short answer is yes we should :)

Because the tests are not working in all the cases we should not generate them.

More details https://github.com/fable-compiler/Fable/issues/1580#issuecomment-428496491

MangelMaxime avatar Oct 10 '18 09:10 MangelMaxime

Hi, I cannot play with turfjs because it relies on geojson. And with generated geojson.ts:

[<RequireQualifiedAccess; CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module GeoJSON =
    let ofGeometry v: GeoJSON = v |> U3.Case1
    let isGeometry (v: GeoJSON) = match v with U3.Case1 _ -> true | _ -> false
    let asGeometry (v: GeoJSON) = match v with U3.Case1 o -> Some o | _ -> None
    let ofFeature v: GeoJSON = v |> U3.Case2
    let isFeature (v: GeoJSON) = match v with U3.Case2 _ -> true | _ -> false

...... I'm getting

  • error FABLE: Cannot type test: options, generic parameters or erased unions
  • error FABLE: Cannot type test: interface "ModuleName.MultiPoint" to name a few

Just to confirm: mine errors are related to the issue and there is no workarround yet?

thanks

citymeterio avatar Nov 07 '18 15:11 citymeterio

You should delete

[<RequireQualifiedAccess; CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module GeoJSON =
    let ofGeometry v: GeoJSON = v |> U3.Case1
    let isGeometry (v: GeoJSON) = match v with U3.Case1 _ -> true | _ -> false
    let asGeometry (v: GeoJSON) = match v with U3.Case1 o -> Some o | _ -> None
    let ofFeature v: GeoJSON = v |> U3.Case2
    let isFeature (v: GeoJSON) = match v with U3.Case2 _ -> true | _ -> false

And others things that are looking similar to that.

MangelMaxime avatar Nov 07 '18 16:11 MangelMaxime

the ofXXX functions can be kept, right?

0x53A avatar Feb 19 '19 20:02 0x53A

I think they still work indeed because they don't "type check against an interface"

MangelMaxime avatar Feb 19 '19 20:02 MangelMaxime