How to fix Required parameter cannot follow optional parameter in TypeScript

TypeScriptINTERMEDIATEMEDIUMUpdated Dec 11, 2025ยท 109 views

This TypeScript error occurs when you define a function with optional parameters followed by required parameters, which violates TypeScript's parameter ordering rules. Optional parameters must come after all required parameters in a function signature. The error helps maintain consistent and predictable function signatures throughout your codebase.

Quick fix

Fixes TS1016

// ERROR: Required parameter cannot follow optional parameter
function greet(name?: string, greeting: string): void {
  // name is optional, greeting is required - WRONG ORDER
  console.log(`${greeting}, ${name || "Guest"}!`);
}
  1. Identify the problematic function signature
  2. Move all optional parameters to the end
  3. Use default parameter values instead of optional syntax
  4. Check method declarations in classes and interfaces
typescript โ€” error
// ERROR: Required parameter cannot follow optional parameter
function greet(name?: string, greeting: string): void {
// name is optional, greeting is required - WRONG ORDER
console.log(`${greeting}, ${name || "Guest"}!`);
}
Required parameter cannot follow optional parameter

What this error means

This TypeScript compiler error occurs when you declare a function or method with an optional parameter (denoted by a question mark `?`) that is followed by a required parameter. In TypeScript, all optional parameters must be placed at the end of the parameter list, after all required parameters. This rule ensures that when calling a function, you can omit optional parameters without needing to provide placeholder values for them. The error prevents ambiguous function signatures where it would be unclear which parameters are being passed when some are omitted. This is a fundamental rule in TypeScript's type system that maintains consistency with JavaScript's handling of optional arguments and prevents runtime errors from incorrect parameter binding.

Typical symptoms

  • TypeScript compiler shows error "TS1016: A required parameter cannot follow an optional parameter"
  • Function definitions with `?` optional parameters are highlighted in red in your IDE
  • Build process fails when running `npm run build` or `tsc`
  • Error message appears when declaring functions with mixed optional and required parameters
  • The error points to the specific function signature with line number
  • Code completion and IntelliSense may show warnings about the parameter order

Common causes

  1. Placing an optional parameter (with `?`) before a required parameter in a function signature
  2. Using TypeScript's optional parameter syntax incorrectly in method declarations
  3. Converting JavaScript code to TypeScript where parameter order wasn't previously enforced
  4. Refactoring function signatures and accidentally reordering parameters incorrectly
  5. Creating function overloads with inconsistent parameter optionality patterns
  6. Using optional parameters in constructor signatures incorrectly
  7. Defining callback types or interface methods with incorrect parameter ordering
  8. Mixing rest parameters (`...args`) with optional parameters in wrong order

How to fix "Required parameter cannot follow optional parameter"

1Identify the problematic function signature

Look at the error message to find which function has the incorrect parameter order. The error will point to the specific line where a required parameter follows an optional one.

typescript
// ERROR: Required parameter cannot follow optional parameter
function greet(name?: string, greeting: string): void {
  // name is optional, greeting is required - WRONG ORDER
  console.log(`${greeting}, ${name || "Guest"}!`);
}
2Move all optional parameters to the end

Rearrange the parameters so all optional parameters (marked with ?) come after all required parameters. This is the fundamental fix for this error.

typescript
// CORRECT: Optional parameters come after required ones
function greet(greeting: string, name?: string): void {
  // greeting is required, name is optional - CORRECT ORDER
  console.log(`${greeting}, ${name || "Guest"}!`);
}

// Now you can call it correctly:
greet("Hello"); // Works: name is optional
greet("Hello", "John"); // Also works
3Use default parameter values instead of optional syntax

Instead of using the ? syntax for optional parameters, consider using default parameter values. This often makes the intent clearer and follows the same ordering rules.

typescript
// Using default values instead of optional syntax
function greet(greeting: string, name: string = "Guest"): void {
  console.log(`${greeting}, ${name}!`);
}

// Both calls work:
greet("Hello"); // Uses default "Guest"
greet("Hello", "John"); // Uses provided name

// This also works with multiple optional parameters:
function createUser(
  username: string, // required
  email: string = "", // optional with default
  isAdmin: boolean = false // optional with default
) {
  // ...
}
4Check method declarations in classes and interfaces

The same rule applies to method declarations in classes, interfaces, and type aliases. Ensure all optional parameters come last in these contexts too.

typescript
interface UserService {
  // ERROR: Optional before required
  findUsers(filter?: string, limit: number): User[];
}

interface UserService {
  // CORRECT: Required before optional
  findUsers(limit: number, filter?: string): User[];
}

class UserRepository {
  // ERROR in class method
  save(user?: User, validate: boolean): void {}
  
  // CORRECT
  save(validate: boolean, user?: User): void {}
}
5Handle function type declarations and callbacks

Function type declarations and callback signatures must also follow the optional parameter ordering rule.

typescript
// Function type declaration
type GreetFunction = (name?: string, greeting: string) => void; // ERROR

type GreetFunction = (greeting: string, name?: string) => void; // CORRECT

// Callback parameter
type Callback = (error?: Error, data: any) => void; // Common Node.js pattern - ERROR!

// Node.js callback pattern should use:
type Callback = (error: Error | null, data?: any) => void; // CORRECT
// OR
type Callback = (error: Error | null | undefined, data: any) => void; // Also works
6Use parameter objects for complex signatures

If you have many optional parameters or complex requirements, consider using a parameter object instead. This avoids ordering issues entirely.

typescript
// Instead of many optional parameters:
function createUser(
  username: string,
  email?: string,
  age?: number,
  isAdmin?: boolean,
  createdAt?: Date
) {
  // Hard to manage, error-prone
}

// Use a parameter object:
interface UserOptions {
  username: string;
  email?: string;
  age?: number;
  isAdmin?: boolean;
  createdAt?: Date;
}

function createUser(options: UserOptions) {
  // All options are accessible via options.property
  // No ordering issues
}

// Clean usage:
createUser({
  username: "john_doe",
  email: "john@example.com",
  isAdmin: true
});
7Fix overloaded function signatures

If using function overloads, ensure each overload signature follows the optional parameter rule. Overloads must be consistent in their parameter ordering.

typescript
// ERROR: Overload with incorrect ordering
function process(input: string, validate?: boolean): string;
function process(input: number, validate?: boolean): number;
function process(input: string | number, validate?: boolean) {
  // Implementation
}

// Actually this is CORRECT since optional is last
// But if you had:
function process(validate?: boolean, input: string): string; // ERROR in overload
function process(validate?: boolean, input: number): number; // ERROR in overload
function process(validate: boolean, input: string | number) {
  // Implementation would be wrong
}
8Consider using rest parameters for variable arguments

If you need a variable number of arguments, consider using rest parameters (...args) instead of multiple optional parameters. Rest parameters must always come last.

typescript
// Instead of many optional parameters:
function join(
  separator?: string,
  ...items: string[] // ERROR: rest parameter must be last
): string {
  return items.join(separator);
}

// CORRECT: Required first, then optional, then rest
function join(
  items: string[], // required
  separator: string = ",", // optional with default
  ...extraOptions: any[] // rest parameter last
): string {
  return items.join(separator);
}

Advanced notes

The "required parameter cannot follow optional parameter" rule exists because of how JavaScript handles function arguments. When you call a function with fewer arguments than parameters, JavaScript fills them from left to right. If optional parameters could come before required ones, you'd need to pass undefined explicitly for the optional parameter to reach the required one, which defeats the purpose of optional parameters. TypeScript's rule aligns with common JavaScript patterns and prevents confusing APIs. However, there are exceptions: in constructor overloads for classes, you might see patterns that appear to violate this rule, but they're actually different constructor signatures. Also, when using tuple types in rest parameters, the ordering rules can be more complex. For callback patterns (like Node.js error-first callbacks), the convention is (error: Error | null, data?: T) => void, which places the required error parameter first, then optional data. Some developers use union types like Error | null | undefined for the first parameter to make it effectively optional while technically being required. Understanding this rule helps design cleaner, more predictable APIs that are easier to use correctly.

Frequently asked questions

What does "TS1016" mean?
) that is followed by a required parameter. In TypeScript, all optional parameters must be placed at the end of the parameter list, after all required parameters. This rule ensures that when calling a function, you can omit optional parameters without needing to provide placeholder values for them.
How do I fix "TS1016"?
This TypeScript error occurs when you define a function with optional parameters followed by required parameters, which violates TypeScript's parameter ordering rules. Optional parameters must come after all required parameters in a function signature. The error helps maintain consistent and predictable function signatures throughout your codebase.
What causes "TS1016"?
Placing an optional parameter (with ?) before a required parameter in a function signature Using TypeScript's optional parameter syntax incorrectly in method declarations Converting JavaScript code to TypeScript where parameter order wasn't previously enforced
Is the "TS1016" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading