Rest Parameters in Functions
Type functions that accept any number of arguments.
Rest Parameters in Functions is a free TypeScript Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Functions With Many Arguments
Rest parameters let a function accept any number of arguments, collected into a single array. TypeScript types that array, giving you safety even with variable-length calls.
Basic Rest Parameter
Prefix the last parameter with ... and give it an array type. Inside the function it behaves like a normal array.
function sum(...nums: number[]): number {
return nums.reduce((a, b) => a + b, 0)
}
console.log(sum(1, 2, 3, 4)) // 10Calling With Zero Arguments
A rest parameter can match zero arguments — the array is simply empty. No special handling is required for the no-argument case.
function sum(...nums: number[]): number {
return nums.reduce((a, b) => a + b, 0)
}
console.log(sum()) // 0Rest After Fixed Parameters
A rest parameter can follow fixed parameters, but it must be last. The fixed ones bind first; everything left over goes into the rest array.
function join(separator: string, ...parts: string[]): string {
return parts.join(separator)
}
console.log(join("-", "a", "b", "c")) // a-b-cOnly One Rest, Always Last
You may have at most one rest parameter, and nothing can follow it. Putting a parameter after the rest is a syntax error.
// function bad(...nums: number[], last: number) {} // Error
function ok(first: number, ...rest: number[]) {
return first + rest.length
}
console.log(ok(10, 1, 2)) // 12Typed Element Constraints
The rest array's element type constrains every argument. Passing a wrong type is caught at compile time.
function tags(...labels: string[]): string {
return labels.join(",")
}
console.log(tags("a", "b"))
// tags("a", 5) // Error: 5 is not a stringRest With a Union Type
The element type can be a union, allowing mixed arguments. Inside, narrow each item before using it.
function show(...items: (string | number)[]): string {
return items.map(String).join(" ")
}
console.log(show("x", 1, "y", 2)) // x 1 y 2Rest With Tuple Types
A rest parameter can be typed as a tuple to describe a fixed-then-variadic shape. Here the first argument is a string, followed by any number of numbers.
function record(...args: [string, ...number[]]): string {
const [label, ...values] = args
return label + ": " + values.join(",")
}
console.log(record("scores", 9, 8, 7))Tuple Rest Enforces Arity
With a tuple rest type, the leading elements are required. Omitting the mandatory first argument is a compile error — stronger than a plain array.
function record(...args: [string, ...number[]]): number {
return args.length
}
console.log(record("x")) // 1
// record() // Error: missing string argumentSpreading Into a Rest Call
You can pass an existing array to a rest parameter using spread at the call site. The array's element type must match the parameter's.
function sum(...nums: number[]): number {
return nums.reduce((a, b) => a + b, 0)
}
const values = [1, 2, 3]
console.log(sum(...values)) // 6Rest in Practice
Rest parameters power logging helpers, formatters, and math utilities — anything that handles a variable count of inputs with a shared or structured type.
function max(first: number, ...rest: number[]): number {
return rest.reduce((m, n) => (n > m ? n : m), first)
}
console.log(max(3, 7, 2, 9, 1)) // 9Quick Check
Test your understanding of typed rest parameters.
Recap
Rest parameters use ... on the last parameter to collect extra arguments into a typed array. They must come last and there can be only one. Type them as a simple array, a union array, or a tuple to express fixed-then-variadic shapes that enforce arity. Spread an existing array at the call site to feed a rest parameter.
Frequently asked questions
Is the “Rest Parameters in Functions” lesson free?
Yes — the full text of “Rest Parameters in Functions” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Rest Parameters in Functions”?
Type functions that accept any number of arguments. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start TypeScript Academy?
No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Rest Parameters in Functions” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this TypeScript Academy lesson?
Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Rest Parameters in Functions
- Spread in Arrays and Objects
- Tuple Rest Elements
- Typing Variadic Functions