I'm facing a challenge with an array declared as as const
:
// example of a simple mock class
class Child { _ = "" }
const child = new Child();
const schema = [[child], child] as const; // readonly [readonly [Child], Child];
This array represents a union of types, where it can be understood as an array of Child
or another array of Child
(with one more level of nesting). Essentially, I aim to convert the type of schema
into (Child | Child[])[]
.
type T = UnwrapAsConstArray<readonly [readonly [Child], Child]> // => (child | Child[])[]
I'm finding it challenging to devise the logic for this transformation. My attempt can be viewed here, but it's not functioning as desired, as shown at the bottom.
For those willing to give it a try, here's a playground where you can experiment with potential solutions, along with some test cases and the expected outcomes.
It's important to note that I require the solution to be recursive and applicable for any level of nesting.
For a related question, check out Create a type tuple from an array without "as const". I'm seeking the "opposite" of that scenario, which I believe is achievable.