How do I handle union types like string | number | boolean on record fields in ReScript?

I have some typescript objects whose fields are typed as string | number | boolean or as boolean | object. If I have to create such a record / object in Rescript before passing it off to Typescript code,
how do I model my record types? I want the values to be unboxed, but @unboxed and @unwrapped do not fit my use case.

type context = Boolean(bool) | Object({val: int, flag: bool})
type t = {
context: context,

I’d like to be able to set a boolean or an object in the record field context. Is there a way in ReScript or a workaround?

one way to do it:

type context
external makeContextBoolean: bool -> context = "%identity"
external makeContextString: string -> context = "%identity"

type t = {
  context: context

I do wish the unbox unwrap stuff was more durable, it seems to only work on immediate external function arguments (iirc) but this comes up all the time youre right.


Thank you so much, this works. The type checks hold up and I can’t mess with the fields.