Annotated solution
Published August 7, 2026The solution
type Flatten<T extends readonly unknown[]> = T extends readonly [infer H, ...infer R] ? H extends readonly unknown[] ? [...Flatten<H>, ...Flatten<R>] : [H, ...Flatten<R>] : []
The common wrong answer
type Flatten<T extends readonly unknown[]> = T extends readonly [infer H, ...infer R] ? H extends readonly unknown[] ? [...H, ...Flatten<R>] : [H, ...Flatten<R>] : []
Spreading `H` directly unwraps exactly one layer, so `[[[1]]]` flattens to `[[1]]` and stops. The head must be flattened recursively too — it is not merely a container to open, it is a whole nested structure.
Line by line
H extends readonly unknown[]The branch that decides whether this element needs opening. `readonly unknown[]` matches both mutable and readonly tuples, so neither is accidentally treated as a scalar.
[...Flatten<H>, ...Flatten<R>]Two recursive calls, one going down into the nesting and one going along the tuple. That pair is what makes the flattening total rather than single-level.
Takeaway
Recursing in one direction walks a list; recursing in two walks a tree. Most "it only went one level deep" bugs are a missing second recursive call.