This TypeScript error appears when a function with required parameters before a rest parameter is called without providing all required arguments. The error message can be misleading since rest parameters themselves don't require arguments.
This error occurs when TypeScript detects that you're calling a function that has one or more required parameters before a rest parameter, but you haven't provided all the required arguments. The confusion arises because the error message mentions the rest parameter name, even though the actual problem is with the required parameters that come before it. Rest parameters (defined with the `...args` syntax) allow functions to accept zero or more additional arguments beyond any required parameters. They're always optional by nature—you can call a function with a rest parameter without passing any extra arguments. However, any non-rest parameters before the rest parameter are still required. This is a somewhat misleading error message in TypeScript's compiler. The compiler is trying to indicate that you haven't provided enough arguments to satisfy the function signature, but the way it phrases the error can make it seem like the rest parameter itself needs arguments, which is not the case.
First, examine the function signature to identify which parameters are required. The rest parameter itself doesn't need arguments—the issue is with required parameters before it.
// Function definition
function greet(greeting: string, ...names: string[]) {
console.log(greeting + " " + names.join(", "));
}
// WRONG - missing required 'greeting' parameter
greet(); // Error: Arguments for the rest parameter 'names' were not provided
// CORRECT - provide the required parameter
greet("Hello"); // OK - rest parameter gets empty array
greet("Hello", "Alice", "Bob"); // OK - rest parameter gets ["Alice", "Bob"]The error message mentions 'names' (the rest parameter) but the real issue is the missing 'greeting' parameter.
Once you've identified the required parameters, make sure to provide them when calling the function:
function processData(action: string, ...items: number[]) {
console.log(`Action: ${action}, Items: ${items}`);
}
// WRONG - missing required 'action' parameter
processData(); // Error
// CORRECT - provide required parameter
processData("sum"); // OK, items = []
processData("sum", 1, 2, 3); // OK, items = [1, 2, 3]Remember: rest parameters accept zero or more arguments, so you don't need to provide any arguments for them specifically.
If a parameter before the rest parameter should be optional, mark it with a question mark or provide a default value:
// Option 1: Use optional parameter with ?
function logMessages(prefix?: string, ...messages: string[]) {
const p = prefix ?? "LOG:";
console.log(p, ...messages);
}
logMessages(); // OK - both optional
logMessages("INFO:"); // OK
logMessages("INFO:", "message1", "message2"); // OK
// Option 2: Use default parameter value
function formatOutput(separator: string = ", ", ...values: string[]) {
return values.join(separator);
}
formatOutput(); // OK - uses default separator
formatOutput("; "); // OK - custom separator
formatOutput("; ", "a", "b", "c"); // OK - custom separator with valuesIf this error appeared after updating dependencies or refactoring, check if the function signature has changed:
// Old signature (still in your code)
function handleEvent(...args: any[]) {
// implementation
}
handleEvent(); // This worked
// New signature (after update/refactor)
function handleEvent(eventType: string, ...args: any[]) {
// implementation
}
handleEvent(); // Now this fails - need to provide eventType
handleEvent("click"); // FixedUse your IDE's "Go to Definition" feature to view the current function signature. If using a library, check the changelog for breaking changes.
If the function has multiple overloads, TypeScript might be selecting the wrong one. Ensure your arguments match the intended overload:
// Multiple overloads
function configure(options: object): void;
function configure(key: string, ...values: string[]): void;
function configure(keyOrOptions: string | object, ...values: string[]): void {
if (typeof keyOrOptions === "object") {
// handle options object
} else {
// handle key + values
}
}
// WRONG - doesn't match any overload properly
configure(); // Error
// CORRECT - matches first overload
configure({ theme: "dark" }); // OK
// CORRECT - matches second overload
configure("theme", "dark"); // OKExplicitly type your arguments or pass them in a way that matches the intended overload.
### Understanding Rest Parameter Syntax
Rest parameters are syntactic sugar for accepting variable numbers of arguments. At runtime, they're converted to an array:
function sum(base: number, ...numbers: number[]) {
return base + numbers.reduce((a, b) => a + b, 0);
}
// Under the hood, this is conceptually similar to:
function sumEquivalent(base: number, numbers: number[]) {
return base + numbers.reduce((a, b) => a + b, 0);
}
sum(10, 1, 2, 3); // base=10, numbers=[1, 2, 3]
sum(10); // base=10, numbers=[]### The Misleading Error Message
This error is documented as Issue #39245 on the TypeScript GitHub repository. The error message is confusing because it mentions the rest parameter when the actual problem is with required parameters before it. The TypeScript team is aware of this UX issue, but the behavior is technically correct—just poorly worded.
### Rest Parameters vs Spread Syntax
Don't confuse rest parameters (in function definitions) with spread syntax (in function calls):
// Rest parameter in definition - collects arguments into array
function myFunc(...args: string[]) {
console.log(args);
}
// Spread syntax in call - expands array into arguments
const items = ["a", "b", "c"];
myFunc(...items); // Same as myFunc("a", "b", "c")### Tuple Rest Parameters (TypeScript 4.0+)
Modern TypeScript supports strongly-typed tuple rest parameters:
function processValues(first: string, ...rest: [number, boolean]) {
console.log(first, rest[0], rest[1]);
}
processValues("test"); // Error - needs exactly 2 more args
processValues("test", 42); // Error - needs 1 more arg
processValues("test", 42, true); // OK
processValues("test", 42, true, "extra"); // Error - too many argsThis provides precise control over the number and types of rest arguments.
### Handling Optional Required Parameters
If you need multiple optional parameters before a rest parameter, consider using an options object instead:
// Instead of this (gets confusing with optionals)
function fetch(url: string, method?: string, headers?: object, ...middlewares: Function[]) {
// ...
}
// Prefer this pattern
interface FetchOptions {
method?: string;
headers?: object;
}
function fetch(url: string, options?: FetchOptions, ...middlewares: Function[]) {
// ...
}
fetch("https://api.example.com"); // Clear and simple
fetch("https://api.example.com", { method: "POST" }); // Options clear
fetch("https://api.example.com", { method: "POST" }, middleware1, middleware2); // All args clearThis pattern is more maintainable and less error-prone.
Function expression requires a return type
Function expression requires a return type
Value of type 'string | undefined' is not iterable
How to fix "Value is not iterable" in TypeScript
Type 'undefined' is not assignable to type 'string'
How to fix "Type undefined is not assignable to type string" in TypeScript
Type narrowing from typeof check produces 'never'
How to fix "Type narrowing produces never" in TypeScript
Type parameter 'T' has conflicting constraints
How to fix "Type parameter has conflicting constraints" in TypeScript