This error means a function or method was called with the wrong number of arguments, and the TypeScript compiler caught it before runtime.

Common Causes and Fixes for TS2554: Expected N Arguments But Got M

1. Missing Required Argument

  • Diagnosis: Look at the function signature in your code or the TypeScript definition file (.d.ts). Identify which arguments are marked as required (not optional or with default values). Compare this to how you’re calling the function.
  • Fix: Add the missing required argument to the function call. For example, if myFunction(a: string, b: number) is called as myFunction("hello"), change it to myFunction("hello", 123).
  • Why it works: TypeScript enforces the function’s contract. Providing all required arguments satisfies this contract.

2. Passing Too Many Arguments

  • Diagnosis: Examine the function signature. If the function doesn’t explicitly accept a rest parameter (...args), then passing more arguments than declared will cause this error.
  • Fix: Remove the excess arguments from the function call. If the function should accept more arguments, you’ll need to update its definition to include them or use a rest parameter. For example, if greet(name: string) is called as greet("Alice", "Smith"), change it to greet("Alice").
  • Why it works: The compiler ensures that callers adhere to the function’s declared interface, preventing potential runtime errors from unused or unexpected arguments.

3. Incorrectly Using Optional Arguments

  • Diagnosis: Functions can have optional arguments (e.g., myFunc(a: string, b?: number)). If you’re trying to pass a value for an optional argument that appears after a required argument that’s missing, TypeScript will flag it as an incorrect argument count.
  • Fix: Ensure all required arguments are passed in order before any optional arguments. If you need to provide an optional argument that comes later in the signature, you might need to pass undefined for the preceding optional arguments. Example: myFunc("required", undefined, "optionalValue").
  • Why it works: TypeScript processes arguments positionally. Omitting a required argument means subsequent arguments are misinterpreted, even if they are intended for optional parameters.

4. Misunderstanding Rest Parameters (...args)

  • Diagnosis: If a function uses a rest parameter (e.g., myFunc(a: string, ...rest: number[])), it expects at least one required argument (a) and then any number of subsequent arguments of the specified type (number[]). If you don’t provide the required arguments, or if you pass non-numeric arguments where numbers are expected in rest, you might get TS2554.
  • Fix: Ensure you pass all declared required arguments before the rest parameter. If the error persists, check the types of the arguments being passed to the rest parameter. Example: myFunc("hello", 1, 2, 3) is correct; myFunc(1, 2, 3) would be incorrect if a is required and not a number.
  • Why it works: Rest parameters collect multiple arguments into an array, but they don’t bypass the requirement for explicitly declared preceding parameters.

5. Incorrectly Overloading Functions

  • Diagnosis: If a function has multiple signatures (overloads), TypeScript matches the call to the most specific signature. If none of the overloads match the number or types of arguments you’re providing, you’ll get TS2554.
  • Fix: Review all overload signatures for the function. Ensure your function call precisely matches one of these signatures. If you’re trying to call it in a way that doesn’t fit any existing overload, you may need to add a new overload or adjust the existing ones.
  • Why it works: Overloads define distinct public interfaces for a function. A call must conform to one of these defined interfaces.

6. Typo in Function Name or Incorrectly Imported Function

  • Diagnosis: Sometimes, the error isn’t about the number of arguments but that you’re calling a function with the wrong name or from the wrong import. If you’ve mistyped a function name, you might be accidentally calling a different function (or a variable) that has a different signature. Similarly, if you’ve imported multiple functions with similar names, you might have imported the wrong one.
  • Fix: Double-check the function name for typos. Verify that you are importing and calling the intended function. Ensure your import statements are correct, especially when dealing with modules that export many items.
  • Why it works: This is a sanity check. Ensuring you’re calling the actual function you intend to call resolves mismatches between your expectation and the actual function’s signature.

7. Incorrect Type Assertion or Generic Usage

  • Diagnosis: If you’re using type assertions (as Type) or working with generic functions, an incorrect assertion or a misunderstanding of how generics handle argument counts can lead to this error. For example, asserting a function that takes no arguments as one that takes one.
  • Fix: Re-evaluate your type assertions. Ensure they accurately reflect the function’s expected signature. If using generics, confirm that the type arguments you’re providing are correct and that the function’s implementation correctly handles those generic types in relation to its arguments.
  • Why it works: Type assertions override the compiler’s checks. If the assertion is wrong, the compiler might not catch the argument mismatch until runtime, but TS2554 is one way it can catch it if the asserted type’s signature is incompatible.

The next error you’ll likely encounter after fixing this is a type mismatch error (TS2345) if the types of the arguments you provided are incorrect, even if the count is right.

Want structured learning?

Take the full Typescript course →