How I unboxed a enum type from object to array?

type t = 
	| A(int, int)
    | B(int)

let a:t = A(100,1)
let b:t = B(2)

The output is:

var a = {
  TAG: /* A */0,
  _0: 100,
  _1: 1
};

var b = {
  TAG: /* B */1,
  _0: 2
};

However, which my expected output is var a = [100, 1] and var b = [2], and the question is coming: How I convert it? I had used @as and @unboxed decorator, but it not works.


I can achieve the above effect with the help of another type, but I wonder if there is a more concise way:

@unboxed
type rec t2 = Any('a): t2

let transform = (t: t): t2 => {
  switch t {
  | A(a1, a2) => Any(a1, a2)
  | B(b) => Any(b)
  }
}

Js.log(transform(A(1, 2))) //  [1,2]
1 Like