Tuple concatenation and reversal
Concept and syntax
Variadic tuple spreads concatenate tuple types. A recursive reverse helper consumes one element at a time and prepends it to an accumulator; its base returns the accumulated tuple. This model targets finite required tuples, not arbitrary arrays.
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 Join<A extends unknown[],B extends unknown[]>=[...A,...B]; type Rev<T extends unknown[],A extends unknown[]=[]>=T extends [infer H,...infer R]?Rev<R,[H,...A]>:A; type Result=Rev<Join<[1,2],[3]>>;const r:Result=[3,2,1]; console.log(JSON.stringify(r));Expected output:
[3,2,1]Common mistake
Intentionally invalid; compile separately and do not execute.
type Join<A extends unknown[],B extends unknown[]>=[...A,...B]; type Rev<T extends unknown[],A extends unknown[]=[]>=T extends [infer H,...infer R]?Rev<R,[H,...A]>:A; type Result=Rev<Join<[1,2],[3]>>;const r:Result=[1,2,3];Expected diagnostic: TS2322. The accumulated tuple reverses positions: 3, 2, 1.
Practice and recap
Trace Rev<["a","b"]> through each accumulator.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Tuple concatenation and reversal — knowledge check
Check the type transformation, trace output, diagnose a rejected example, and apply the concept to a new case.
Which statement about Tuple concatenation and reversal is accurate?
What is the complete stdout, represented as a JSON string (including newline characters)?
type Join<A extends unknown[],B extends unknown[]>=[...A,...B]; type Rev<T extends unknown[],A extends unknown[]=[]>=T extends [infer H,...infer R]?Rev<R,[H,...A]>:A; type Result=Rev<Join<[1,2],[3]>>;const r:Result=[3,2,1]; console.log(JSON.stringify(r));Which diagnostic and explanation match the rejected example under the lesson settings?
type Join<A extends unknown[],B extends unknown[]>=[...A,...B]; type Rev<T extends unknown[],A extends unknown[]=[]>=T extends [infer H,...infer R]?Rev<R,[H,...A]>:A; type Result=Rev<Join<[1,2],[3]>>;const r:Result=[1,2,3];What is Rev<[]> with its default accumulator?