This TypeScript error occurs when you declare a rest parameter with a type that is not an array type. Rest parameters must be typed as arrays (T[] or Array<T>) or tuples to accept multiple arguments. The fix involves correcting the type annotation to use proper array syntax.
In TypeScript, rest parameters (denoted by ...parameterName) allow functions to accept an indefinite number of arguments as an array. The language requires that rest parameters be explicitly typed as array types because they represent collections of values, not single values. When you see "Rest parameter must be of an array type", TypeScript is telling you that the type annotation following the rest parameter syntax must be an array type (like number[], string[], or Array<T>) or a tuple type (like [string, number]), not a primitive type like number or string.
Examine the function definition and look at the rest parameter's type annotation. Rest parameters must use array types:
// ❌ WRONG - primitive type
function sum(...numbers: number) {
// Error: Rest parameter must be of an array type
}
// ✅ CORRECT - array type
function sum(...numbers: number[]) {
return numbers.reduce((a, b) => a + b, 0);
}Make sure your type ends with [] or uses Array<T> syntax.
If you intended to accept multiple values of a certain type, add array brackets:
// Before (error):
function logMessages(...messages: string) {
messages.forEach(msg => console.log(msg));
}
// After (fixed):
function logMessages(...messages: string[]) {
messages.forEach(msg => console.log(msg));
}
// Or using Array<T> syntax:
function logMessages(...messages: Array<string>) {
messages.forEach(msg => console.log(msg));
}Both string[] and Array<string> are valid array type annotations.
If you need a specific number of arguments with specific types, use tuple types:
// Tuple type for exactly two arguments
function processPair(...pair: [string, number]) {
const [name, value] = pair;
console.log(`${name}: ${value}`);
}
// Variable-length tuple with rest element
function processWithOptional(...args: [string, ...number[]]) {
const [name, ...values] = args;
console.log(`${name} has ${values.length} values`);
}Tuple types are also considered "array types" for rest parameters.
Ensure the rest parameter is the last parameter in your function:
// ❌ WRONG - rest parameter not last
function process(a: number, ...rest: number[], b: string) {
// Error: A rest parameter must be last in a parameter list
}
// ✅ CORRECT - rest parameter is last
function process(a: number, b: string, ...rest: number[]) {
// Works correctly
}Rest parameters must always come after all regular parameters.
After fixing the type, test your function with various numbers of arguments:
// Fixed function
function calculateAverage(...numbers: number[]) {
if (numbers.length === 0) return 0;
const sum = numbers.reduce((a, b) => a + b, 0);
return sum / numbers.length;
}
// Test cases
console.log(calculateAverage()); // 0
console.log(calculateAverage(10)); // 10
console.log(calculateAverage(10, 20)); // 15
console.log(calculateAverage(10, 20, 30)); // 20
// Works with spread syntax too
const scores = [85, 90, 78, 92];
console.log(calculateAverage(...scores)); // 86.25The function should now accept any number of arguments of the specified type.
In TypeScript, rest parameters are particularly useful with generic functions where you might not know the exact type in advance. You can use generic array types like ...args: T[] where T is a type parameter. Also note that while JavaScript allows rest parameters without type annotations, TypeScript requires explicit array type annotations for type safety. When migrating JavaScript code to TypeScript, this is a common error that appears during the type annotation phase.
Type parameter 'X' is not used in the function signature
How to fix "Type parameter not used in function signature" in TypeScript
Type parameter 'X' is defined but never used
How to fix "Type parameter is defined but never used" in TypeScript
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