How to fix Cannot use 'arguments' in this context in TypeScript

TypeScriptINTERMEDIATEMEDIUMUpdated Dec 8, 2025ยท 102 views

This error occurs when trying to use the 'arguments' object in arrow functions or certain class contexts where it is not available. Modern TypeScript prefers rest parameters as a type-safe alternative.

Quick fix

Fixes Cannot use 'arguments' in this context

class Logger {
  log = () => {
    console.log(arguments); // Error: Cannot use 'arguments' in this context
  };
}
  1. Replace arrow functions with regular functions
  2. Use rest parameters instead (recommended)
  3. Use rest parameters in arrow functions
  4. Type rest parameters properly
typescript โ€” error
class Logger {
log = () => {
console.log(arguments); // Error: Cannot use 'arguments' in this context
};
}
Cannot use 'arguments' in this context

What this error means

The `arguments` object is a legacy JavaScript feature that contains all parameters passed to a function, but it has significant limitations in modern JavaScript and TypeScript: **Arrow Functions**: Arrow functions do not have their own `arguments` object. When you try to access `arguments` inside an arrow function, it attempts to reference the `arguments` from the enclosing scope, which often doesn't exist. **Class Methods**: When using arrow function syntax for class methods (e.g., `methodName = () => {}`), the `arguments` object is unavailable because arrow functions don't bind their own function context. **Strict Mode**: Accessing the legacy `arguments` object in strict mode (which TypeScript enforces) is restricted for security reasons. In modern TypeScript, **rest parameters** (`...args`) are the recommended solution. Rest parameters are explicitly declared as part of the function signature, provide full array functionality, and work in all function contexts.

Typical symptoms

  • Compiler error when trying to use 'arguments' in an arrow function
  • Type error when accessing 'arguments' in a class method
  • Runtime error: 'arguments is not defined'
  • Error message includes 'Cannot use arguments in this context'
  • Works in traditional functions but fails in arrow functions

Common causes

  1. Using 'arguments' inside an arrow function (arrow functions don't have their own arguments object)
  2. Using 'arguments' in an arrow function class method (most common)
  3. Attempting to access 'arguments' in a context where it's not bound
  4. Strict mode restrictions on legacy 'arguments' usage
  5. TypeScript configuration not allowing legacy 'arguments' patterns

How to fix "Cannot use 'arguments' in this context"

1Replace arrow functions with regular functions

Convert arrow functions to traditional function syntax to access arguments:

Before (Error):

typescript
class Logger {
  log = () => {
    console.log(arguments); // Error: Cannot use 'arguments' in this context
  };
}

After (Works):

typescript
class Logger {
  log() {
    console.log(arguments); // Works in traditional method
  };
}

This applies to both class methods and standalone functions.

2Use rest parameters instead (recommended)

Replace the arguments object with rest parameters (...args). This is the modern, type-safe approach:

Before:

typescript
function processValues() {
  for (let i = 0; i < arguments.length; i++) {
    console.log(arguments[i]);
  }
}

After:

typescript
function processValues(...args: any[]) {
  for (let i = 0; i < args.length; i++) {
    console.log(args[i]);
  }
}

With proper typing:

typescript
function processValues(...args: string[]) {
  args.forEach(arg => console.log(arg));
}

processValues("hello", "world");
3Use rest parameters in arrow functions

Arrow functions work perfectly with rest parameters:

Before (Error):

typescript
const handleClick = () => {
  console.log(arguments); // Error!
};

After (Works):

typescript
const handleClick = (...args: Event[]) => {
  console.log(args);
};

This is especially useful for React event handlers and callbacks:

typescript
const handleChange = (...args: [React.ChangeEvent<HTMLInputElement>]) => {
  const [event] = args;
  console.log(event.target.value);
};
4Type rest parameters properly

Always provide type annotations for rest parameters in TypeScript:

Variadic (multiple types):

typescript
function mix(...args: (string | number)[]): void {
  args.forEach(arg => console.log(typeof arg, arg));
}

Tuple rest parameters (specific order):

typescript
function request(method: string, url: string, ...options: [headers?: Record<string, string>, timeout?: number]) {
  // method: string, url: string, options[0]: headers?, options[1]: timeout?
}

Spread with specific types:

typescript
function sum(...numbers: number[]): number {
  return numbers.reduce((a, b) => a + b, 0);
}
5Migrate common arguments patterns

Here are translations for common arguments usage patterns:

Get all arguments as array:

typescript
// Old
const allArgs = Array.from(arguments);

// New
const allArgs = [...args]; // or just args

Get first argument:

typescript
// Old
const first = arguments[0];

// New
const [first, ...rest] = args;

Get last argument:

typescript
// Old
const last = arguments[arguments.length - 1];

// New
const last = args[args.length - 1];

Check argument count:

typescript
// Old
if (arguments.length === 0) { ... }

// New
if (args.length === 0) { ... }

Advanced notes

### Why Rest Parameters are Better

1. Type Safety: Rest parameters are properly typed, providing IDE autocomplete and compile-time checking.

2. Function Signature Clarity: The function signature explicitly shows it accepts variadic arguments, making the code self-documenting.

3. Array Methods: Rest parameters are real arrays, supporting all array methods (map, filter, forEach, etc.). The arguments object is array-like but lacks these methods.

4. Better Performance: Modern JavaScript engines optimize rest parameters better than the legacy arguments object.

### Handling Mixed Required and Optional Parameters

typescript
// Mix required and rest parameters
function greet(greeting: string, ...names: string[]): void {
  console.log(`${greeting}, ${names.join(" and ")}!`);
}

greet("Hello", "Alice", "Bob", "Charlie");

### Destructuring with Rest Parameters

typescript
function processData(first: string, second: string, ...rest: any[]): void {
  console.log("First:", first);
  console.log("Second:", second);
  console.log("Rest:", rest);
}

const data = ["a", "b", "c", "d"];
processData(...data);

### Default Parameters with Rest

typescript
function format(prefix: string = "INFO", ...messages: string[]): void {
  console.log(`[${prefix}] ${messages.join(" ")}`);
}

format(); // [INFO]
format("ERROR", "Something", "failed");

### Class Method Example

typescript
class EventEmitter {
  // Wrong - arrow function with arguments
  // emit = () => console.log(arguments); // Error!

  // Correct - regular method with rest parameters
  emit(eventName: string, ...args: any[]): void {
    console.log(`Event: ${eventName}`, args);
  }
}

Frequently asked questions

What does "Cannot use 'arguments' in this context" mean?
The arguments object is a legacy JavaScript feature that contains all parameters passed to a function, but it has significant limitations in modern JavaScript and TypeScript: Arrow Functions: Arrow functions do not have their own arguments object. When you try to access arguments inside an arrow function, it attempts to reference the arguments from the enclosing scope, which often doesn't exist. , methodName = () => {}), the arguments object is unavailable because arrow functions don't bind their own function context.
How do I fix "Cannot use 'arguments' in this context"?
This error occurs when trying to use the 'arguments' object in arrow functions or certain class contexts where it is not available. Modern TypeScript prefers rest parameters as a type-safe alternative.
What causes "Cannot use 'arguments' in this context"?
Using 'arguments' inside an arrow function (arrow functions don't have their own arguments object) Using 'arguments' in an arrow function class method (most common) Attempting to access 'arguments' in a context where it's not bound
Is the "Cannot use 'arguments' in this context" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading