Senior LevelJavaScript & TypeScript
What are TypeScript variadic tuple types and tuple manipulation?
❓ Interview Prompt: Explain variadic tuple syntax (...T), spread in tuple types, concat/push/pop tuple type transformations.
Variadic Tuple Types allow generic type parameters to be spread (...T) inside tuple definitions, enabling flexible type manipulation of function parameters and array tuples.
Use Cases:
•
Typing function currying and composition.
•
Concatenating two unknown tuple types:
type Concat<T extends any[], U extends any[]> = [...T, ...U];•
Prepending or appending elements to tuple signatures.
Code Implementation Example
typescript
// Tuple Concatenation type
type Concat<T extends readonly any[], U extends readonly any[]> = [...T, ...U];
type Tuple1 = [number, string];
type Tuple2 = [boolean, Date];
type Combined = Concat<Tuple1, Tuple2>; // [number, string, boolean, Date]
// Typing function prefixing via variadic tuples
function prefixArg<T extends any[], R>(
fn: (...args: T) => R,
prefix: string
): (prefix: string, ...args: T) => R {
return (prefix, ...args) => fn(...args);
}💡Variadic tuple type spreading for tuple concatenation and function parameter composition.
🎯 Interview Key Takeaway
Variadic tuple types allow spreading generic arrays inside tuple annotations to create flexible parameter signatures.
#TypeScript#Variadic Tuples#Generics#Senior
Back to all JavaScript & TypeScript Questions→