This error means TypeScript couldn’t find a function signature that matches the arguments you’re trying to pass.
Here’s what’s likely happening and how to fix it, from most to least common:
-
Incorrect Argument Types: You’re passing a value of one type where another is expected.
- Diagnosis: Hover over the function call in your IDE. TypeScript will show you the available overloads and highlight which parameter types don’t match. You can also use
tsc --noEmit --strictin your terminal to get detailed error messages. - Fix: Adjust the type of the argument to match what the function expects. For example, if a function expects a
numberand you’re passing astring, convert the string:myFunction(parseInt("123"));ormyFunction(Number("123"));. - Why it works: TypeScript enforces type safety. By providing the correct type, you satisfy the function’s signature.
- Diagnosis: Hover over the function call in your IDE. TypeScript will show you the available overloads and highlight which parameter types don’t match. You can also use
-
Missing Optional Arguments or Spread Operator Issues: You might be omitting an optional argument that’s required in a specific overload, or misusing the spread operator (
...).- Diagnosis: Check the function’s definition for optional parameters (marked with
?) or rest parameters. See which overload requires them. If using spread, ensure the iterable you’re spreading has the correct element type. - Fix:
- If an optional argument is missing, provide it. If it’s a complex object, provide an empty object
{}if that’s valid for that overload. - If using a spread operator, ensure the source array/object matches the expected type for the rest parameter. For instance, if a function expects
...args: string[], ensure you’re spreading an array of strings.
- If an optional argument is missing, provide it. If it’s a complex object, provide an empty object
- Why it works: Overloads can have different numbers of parameters. Providing all required parameters or correctly spreading an array ensures you’re hitting an overload that accommodates your argument count.
- Diagnosis: Check the function’s definition for optional parameters (marked with
-
Typo in Function Name or Property Access: A simple typo can lead TypeScript to believe you’re calling a non-existent function or accessing a property with the wrong name.
- Diagnosis: Carefully re-read the function name and any property access chains leading to it. Check for common typos (e.g.,
lenghtinstead oflength). - Fix: Correct the spelling of the function name or property.
- Why it works: Ensures you are actually referencing the intended function or property.
- Diagnosis: Carefully re-read the function name and any property access chains leading to it. Check for common typos (e.g.,
-
Mismatched Generics: If the function uses generics, you might be providing an argument that doesn’t satisfy the generic constraints, or the generic type argument itself is inferred incorrectly.
- Diagnosis: Look for angle brackets (
<T>) in the function signature. Hover over the call site to see how TypeScript is inferringT. If it’s inferringanyor a type that doesn’t fit, that’s the problem. - Fix: Explicitly provide the generic type argument if inference is failing. For example:
myGenericFunction<MySpecificType>(someValue);. EnsuresomeValueis assignable toMySpecificType. - Why it works: Explicitly defining the generic type parameter guides TypeScript to find a matching overload that is specialized for that type.
- Diagnosis: Look for angle brackets (
-
Incorrect Module Import or Namespace Usage: The function might be defined in a different module or namespace than you’re importing it from, or you might be missing a default import/namespace export.
- Diagnosis: Verify your
importstatements. Check the source file of the function to see how it’s exported (e.g.,export function ...,export default function ...,export const ...). - Fix: Adjust your import statement. If it’s a default export, use
import MyFunction from './myModule';. If it’s a named export, useimport { MyFunction } from './myModule';. If it’s part of a namespace, ensure you’re accessing it correctly (e.g.,MyNamespace.MyFunction). - Why it works: Guarantees that the function you’re trying to call is actually the one you’ve brought into scope.
- Diagnosis: Verify your
-
Library Version Mismatch or Incompatible Declaration Files: You might be using a library with a version that doesn’t match its TypeScript declaration files (
.d.ts), or the declarations themselves are faulty.- Diagnosis: Check the
package.jsonfor the library’s version. Compare it to the version of the@types/library-namepackage if you’re using separate type definitions. Look at the library’s documentation for API changes between versions. - Fix:
- Ensure your library version and its corresponding type definitions are compatible. Update or downgrade as necessary.
- If the declarations are faulty, you might need to report an issue to the library maintainers or provide your own augmentation.
- Try deleting
node_modulesandpackage-lock.json(oryarn.lock) and reinstalling:rm -rf node_modules package-lock.json && npm install.
- Why it works: Realigns the runtime code with the static type information TypeScript uses for checking.
- Diagnosis: Check the
-
Complex Union/Intersection Types Not Satisfying a Constraint: The value you’re passing might be a union or intersection type that, when evaluated, doesn’t perfectly match any of the expected parameter types in any overload.
- Diagnosis: Examine the type of the variable you’re passing. If it’s a union (e.g.,
string | number), check if all members of the union are compatible with at least one of the function’s parameter types across its overloads. This is less common for simple calls but can happen with complex types. - Fix: Introduce a type guard or a temporary variable to narrow down the type before passing it to the function. For example:
function process(arg: string | number) { if (typeof arg === 'string') { myFunction(arg); // Assuming myFunction has an overload for string } else { myFunction(arg); // Assuming myFunction has an overload for number } } - Why it works: Type guards allow TypeScript to understand that a specific branch of execution guarantees a narrower, compatible type.
- Diagnosis: Examine the type of the variable you’re passing. If it’s a union (e.g.,
The next error you’ll likely encounter after fixing this is a TS2345: Argument of type 'X' is not assignable to parameter of type 'Y'. if you’ve corrected one overload but still have type mismatches elsewhere.