Sub topic 13
Tuples as Function Parameter Lists
Assume you know basic TypeScript function types, tuple syntax, optional and rest parameters, and union types. In TypeScript, a tuple can describe a function's complete parameter list. This lets you store arguments in a variable, pass them with spread syntax, and transform parameter lists in generic code.
Representing parameters with a tuple
Suppose a function connects to a server:
function connect(host: string, port: number): void {
console.log(`Connecting to ${host}:${port}`);
}
type ConnectArgs = [host: string, port: number];
const args: ConnectArgs = ["localhost", 8080];
connect(...args);ConnectArgs has two positions. The first must be a string, and the second must be a number. The spread expression ...args expands the tuple into the two arguments expected by connect.
A normal array type would lose this positional information:
const values: (string | number)[] = ["localhost", 8080];
// connect(...values); // Error: the array might not have the right positionsThe array says that every element is either a string or a number. It does not say which type belongs at each position or how many elements exist.
Deriving a parameter tuple
The built-in Parameters utility type extracts a function's parameter list as a tuple:
function sendMessage(to: string, body: string, urgent: boolean): void {
console.log({ to, body, urgent });
}
type SendMessageArgs = Parameters<typeof sendMessage>;
// [to: string, body: string, urgent: boolean]
const messageArgs: SendMessageArgs = [
"[email protected]",
"Build completed",
true,
];
sendMessage(...messageArgs);This keeps the tuple synchronized with the function declaration. If sendMessage later gains another parameter or changes a parameter type, SendMessageArgs changes too.
The typeof operator here refers to the type of the function named sendMessage. Parameters then reads that function type and returns its parameter tuple.
Forwarding arguments
A wrapper function can use a generic tuple to forward arguments without losing their types:
function logCall<T extends unknown[], R>(
fn: (...args: T) => R,
...args: T
): R {
console.log("Calling function");
return fn(...args);
}
function multiply(a: number, b: number): number {
return a * b;
}
const result = logCall(multiply, 6, 7);
// result: numberT extends unknown[] means that T must be some array or tuple type. TypeScript infers T as [number, number] for this call. The same tuple type describes both the wrapper's rest parameter and the arguments passed to fn.
This pattern is useful for logging, caching, retries, and event handlers. The wrapper can add behavior while preserving the original function's parameter checking and return type.
Optional and rest parameters
Tuple parameter lists can include optional and rest elements:
type SearchArgs = [query: string, limit?: number];
function search(...args: SearchArgs): string[] {
const [query, limit = 10] = args;
return [`Searching for "${query}" with limit ${limit}`];
}
search("typescript");
search("typescript", 20);
type LogArgs = [message: string, ...details: unknown[]];
function log(...args: LogArgs): void {
console.log(...args);
}
log("User created", { id: 42 }, true);The optional element must come after required elements. A rest element must be last and can contain zero or more values.
Visual model
Exercise
What tuple type correctly represents a function declared as function open(path: string, readOnly: boolean): void?
Show answer
type OpenArgs = [path: string, readOnly: boolean];