Generated tests for erased unions not working with Fable 2
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?
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
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
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.
the ofXXX functions can be kept, right?
I think they still work indeed because they don't "type check against an interface"