Tuple Rest Elements

What

Rest elements allow tuples to have a variable number of elements of a specific type.

How

You use the spread operator (...) followed by an array type within the tuple definition:

type Command = [name: string, ...args: string[]];

const git: Command = ["git", "commit", "-m", "init"];
const npm: Command = ["npm", "install"];

Why

Rest elements are useful for modeling structures that have a fixed-length prefix but an arbitrary number of trailing elements, such as a CLI command name followed by its arguments. This allows you to maintain type safety for the “header” while remaining flexible for the “payload”.

Summary

Rest elements bridge the gap between fixed-length tuples and variable-length arrays.