TypeScript Concepts
Type-level recursion — recursive type computation
A recursive type can refer to itself to describe nested data or compute a result. A recursive conditional needs a meaningful base case and decreasing work on intended inputs. The example unwraps nested array element types without walking any runtime array. Complex inputs can hit compiler instantiation limits. Observed output: '7'. Local example verified with TypeScript 5.8.3, strict checking, ES2022/CommonJS and Node.js 22.16.0; declarations enabled.
Formal Syntax & Structure
typescript syntaxtypescript
type Flatten<T> = T extends readonly (infer E)[] ? Flatten<E> : T;Working Example
Example Snippettypescript
type Flatten<T> = T extends readonly (infer E)[] ? Flatten<E> : T;const item: Flatten<readonly (number[])[]> = 7;console.log(item);