Array and tuple inference
Concept and syntax
A nonempty tuple pattern can infer its first element and remaining tuple. readonly in the pattern accepts readonly input tuples; the inferred rest is not automatically readonly. An arbitrary array need not be nonempty, so use a separate array element pattern when appropriate.
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 Split<T>=T extends readonly [infer H,...infer R]?[H,R]:never;const x:Split<readonly [1,"tea",true]>=[1,["tea",true]]; console.log(JSON.stringify(x));Expected output:
[1,["tea",true]]Common mistake
Intentionally invalid; compile separately and do not execute.
type Split<T>=T extends readonly [infer H,...infer R]?[H,R]:never;const x:Split<[]>=[1,[]];Expected diagnostic: TS2322. The empty tuple does not match a required head.
Practice and recap
Add a special empty-tuple branch.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Array and tuple inference — knowledge check
Check the type transformation, trace output, diagnose a rejected example, and apply the concept to a new case.
Which statement about Array and tuple inference is accurate?
What is the complete stdout, represented as a JSON string (including newline characters)?
type Split<T>=T extends readonly [infer H,...infer R]?[H,R]:never;const x:Split<readonly [1,"tea",true]>=[1,["tea",true]]; console.log(JSON.stringify(x));Which diagnostic and explanation match the rejected example under the lesson settings?
type Split<T>=T extends readonly [infer H,...infer R]?[H,R]:never;const x:Split<[]>=[1,[]];Why does Split<number[]> not infer a head?