How to fix Rest parameter must be of an array type in TypeScript
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.
Quick fix
Fixes Rest parameter must be of an array type
// ❌ 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);
}- Check your rest parameter type annotation
- Convert primitive types to array types
- Use tuple types for fixed-length rest parameters
- Verify rest parameter position in function signature
// ❌ WRONG - primitive typefunction sum(...numbers: number) {// Error: Rest parameter must be of an array type}// ✅ CORRECT - array typefunction sum(...numbers: number[]) {return numbers.reduce((a, b) => a + b, 0);}Rest parameter must be of an array type
What this error means
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.
Typical symptoms
- TypeScript compiler shows error TS2370: "Rest parameter must be of an array type"
- Code fails to compile with red squiggly lines under the rest parameter
- Function definitions with ...parameter: SomeType fail to build
- IDE shows type error when hovering over the rest parameter
Common causes
- Using a primitive type (number, string, boolean) instead of an array type for a rest parameter
- Forgetting to add array brackets [] after the type in the rest parameter annotation
- Confusing rest parameters with regular parameters that accept single values
- Copy-pasting code from JavaScript without adding proper TypeScript type annotations
How to fix "Rest parameter must be of an array type"
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.
Advanced notes
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.
Frequently asked questions
- What does "Rest parameter must be of an array type" mean?
- 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.
- How do I fix "Rest parameter must be of an array type"?
- 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.
- What causes "Rest parameter must be of an array type"?
- Using a primitive type (number, string, boolean) instead of an array type for a rest parameter Forgetting to add array brackets [] after the type in the rest parameter annotation Confusing rest parameters with regular parameters that accept single values
- Is the "Rest parameter must be of an array type" error serious?
- It is a low-severity error — more of a blocker than a risk. The steps above resolve it.