Instantiation limits
Concept and syntax
Recursive type computation has compiler resource limits. A tuple builder with a reachable length base case can work for small literal sizes; a self-call that makes no progress cannot finish. The exact depth and complexity budgets are implementation details, so bound inputs and simplify expensive types.
Working example
Save each snippet separately as main.ts in a fresh directory with TypeScript 5.8.3. Compile with tsc --strict --target ES2022 --module commonjs --moduleDetection force --skipLibCheck --noEmitOnError main.ts and run accepted snippets with node main.js. Type aliases are erased; assignments and diagnostic checks demonstrate the type relations, not the printed output alone.
type Build<N extends number,A extends unknown[]=[]>=A["length"] extends N?A:Build<N,[...A,unknown]>;const a:Build<3>=[1,2,3]; console.log(a.length);Expected output:
3Common mistake
Intentionally invalid; compile separately and do not execute.
type Build<N extends number,A extends unknown[]=[]>=A["length"] extends N?A:Build<N,[...A,unknown]>;type Loop<T>=T extends unknown?Loop<T>:never; type Bad=Loop<1>;Expected diagnostic: TS2589. The conditional recurses without progress, exceeding instantiation limits.
Practice and recap
Add a finite depth parameter to a recursive helper.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Instantiation limits — knowledge check
Check the type transformation, trace output, diagnose a rejected example, and apply the concept to a new case.
Which statement about Instantiation limits is accurate?
What is the complete stdout, represented as a JSON string (including newline characters)?
type Build<N extends number,A extends unknown[]=[]>=A["length"] extends N?A:Build<N,[...A,unknown]>;const a:Build<3>=[1,2,3]; console.log(a.length);Which diagnostic and explanation match the rejected example under the lesson settings?
type Build<N extends number,A extends unknown[]=[]>=A["length"] extends N?A:Build<N,[...A,unknown]>;type Loop<T>=T extends unknown?Loop<T>:never; type Bad=Loop<1>;Which modification improves termination of Loop?