Polymorphic variants - converting polymorphic variant type to type with lower bound constraint

This works:

# f: unit => [> #A]
let f = () => #A     
# f: unit => [> #B]
let g = () => #B

# h: int => [> #A | #B]
let h = i => i == 1 ? f() : g()

and the result of h is int => [> #A | #B]

Now I want to limit f and g to return exact types:

let f: unit => [#A] = () => #A
let g: unit => [#B] = () => #B

let h = i => i == 1 ? f() : g()
                            ^-^
  This has type: [#B]
  Somewhere wanted: [#A]
  These two variant types have no intersection

How do I make h to return [> #A|#B] as in first example?

You need an explicit coercion:

let h b = if b then (f():[`A]:>[>`A]) else (g():[`B]:>[>`B])

Thank you very much!. Very sorry for posting Rescript code. Got totally confused by where I am and what language it is.

1 Like

Super sorry again.