Typed tuple concatenation
Combine two parameter or data sequences while retaining each tuple element's position and type.
TypeScript · Function
Variadic tuple types let generic spreads capture and rebuild tuple or array sequences. They support tuple concatenation, fixed elements around variable segments, and strongly typed function composition.
A variadic tuple type uses spread syntax in a tuple declaration to represent an unknown tuple segment. Generic spreads let a function capture tuple types and reconstruct a new tuple by placing those segments before, after, or around other elements.
type Result = [...T, ...U]| Name | Type | Description |
|---|---|---|
T | T extends readonly any[] | First tuple or array sequence to preserve and place in the output. |
U | U extends readonly any[] | Second tuple or array sequence to preserve and place after the first sequence. |
A tuple or array sequence formed by spreading T followed by U: [...T, ...U].
type Arr = readonly unknown[];
function concat<T extends Arr, U extends Arr>(
first: T,
second: U,
): [...T, ...U] {
return [...first, ...second];
}
const result = concat([1, "a"] as const, [true] as const);
// ^? readonly [1, "a"] is accepted as T
// result: [1, "a", true]The generic tuple types preserve each input tuple, while the returned spread types model their concatenation.
Combine two parameter or data sequences while retaining each tuple element's position and type.
Describe APIs with fixed boundary parameters and an arbitrary number of parameters between them.
Capture one function's parameters and reconstruct them while composing or transforming functions.
A variadic tuple is a tuple type containing a spread segment, such as [...T, ...U]. Generic spreads allow TypeScript to preserve the concrete tuple types supplied to a function and construct a corresponding output tuple.
Follow-up: How would you type concatenation? Constrain both generics to array types and return [...T, ...U], which models the runtime concatenation.
They allow tuple spreads to be generic, so a type can represent operations over tuples whose element types are not known when the declaration is written. Instantiating the generics produces concrete tuple or array types.
Follow-up: What happens if a generic spread is an array with unknown length? The resulting type becomes unbounded, and later elements are absorbed into its rest element type.
Rest elements may appear in the middle of a tuple, enabling signatures with a fixed prefix, a variable segment, and a fixed suffix. This supports APIs whose parameters have known boundary elements but an unknown number of intervening arguments.
Follow-up: Can an unconstrained rest segment preserve later fixed positions? No: if the segment has unknown length, later positions cannot remain separately identifiable in the resulting type.
Tuple TypesRest Elements in Tuple TypesGeneric Rest ParametersFunction Compositionreadonly tuples