Course contentsShow
← TypeScript
Lesson 264 of 1,7966. FunctionsFree lesson

Combining Fixed and Rest Parameters

Explore patterns for mixing required parameters with rest parameters and how TypeScript validates the order.

Combining Fixed and Rest Parameters

You'll learn how to use required parameters alongside rest parameters in a single function signature, and the strict ordering rule TypeScript enforces.

The Core Pattern

You can mix fixed (required) parameters with rest parameters in the same function, but TypeScript enforces a critical rule: rest parameters must always come last. This makes logical sense—if TypeScript allowed rest parameters in the middle, it wouldn't know where the "rest" ends and the next parameter begins.

Think of it like filling containers: you fill specific labeled bottles first (fixed parameters), then pour everything remaining into one big bucket (rest parameter).

How It Works

function greetUsers(greeting: string, ...names: string[]): string {
  return `${greeting} ${names.join(', ')}!`;
}

greetUsers("Hello", "Alice", "Bob", "Charlie");
// "Hello Alice, Bob, Charlie!"

Here greeting is a required parameter, and ...names captures all remaining arguments as an array.

You can have multiple fixed parameters before the rest parameter:

function logMessage(level: string, timestamp: number, ...details: string[]): void {
  console.log(`[${level}] ${timestamp}: ${details.join(' ')}`);
}

logMessage("ERROR", Date.now(), "Database", "connection", "failed");

The Ordering Rule

TypeScript will reject any attempt to place a rest parameter before other parameters:

// ❌ Error: A rest parameter must be last
function invalid(...items: string[], category: string) {}

Key Takeaway: Rest parameters must always be the final parameter in a function signature, but you can have as many fixed parameters before them as needed—TypeScript validates this order strictly.