How to fix Argument of type 'X' is not assignable to parameter of type 'Y' in TypeScript

TypeScriptINTERMEDIATEHIGHUpdated Dec 8, 2025ยท 180 views

This TypeScript error occurs when you pass an argument to a function whose type does not match the function parameter's expected type. It's a strict type checking error that prevents incompatible values from being passed to functions. Understanding type compatibility and using proper type annotations resolves this common issue.

Quick fix

Fixes TS2345

// Error: Argument of type 'string' is not assignable to parameter of type 'number'
function add(a: number, b: number): number {
  return a + b;
}

add("5", 10); // WRONG - first argument is string, not number
  1. Read the error message carefully to identify the exact types
  2. Check if you're passing the correct type
  3. Ensure object properties match the expected type
  4. Handle null and undefined with proper type guards
typescript โ€” error
// Error: Argument of type 'string' is not assignable to parameter of type 'number'
function add(a: number, b: number): number {
return a + b;
}
add("5", 10); // WRONG - first argument is string, not number
Argument of type 'X' is not assignable to parameter of type 'Y'

What this error means

This error indicates a type mismatch between the argument you are passing to a function and the type of the parameter that function expects. TypeScript's type checker has determined that the type of your argument (X) is not compatible with the declared parameter type (Y). This is a fundamental type safety check that prevents passing invalid values to functions. The error can occur in various scenarios: passing a string where a number is expected, passing a value that might be null where null is not allowed, passing an object missing required properties, or passing a value of a completely different type. The strict type checking catches these errors at compile time rather than allowing them to cause runtime failures.

Typical symptoms

  • TypeScript compiler shows error "TS2345: Argument of type 'X' is not assignable to parameter of type 'Y'"
  • Function calls are highlighted in red in your IDE
  • Build process fails when running `npm run build` or `tsc`
  • Error message appears even though the code might run without crashing
  • The error points to a specific function call with a line number
  • Related types are shown in the error message for debugging

Common causes

  1. Passing a value of the wrong primitive type (string instead of number, boolean instead of string, etc.)
  2. Passing a value that might be null or undefined where those types are not accepted by the function
  3. Passing an object that is missing required properties or has extra properties the function doesn't expect
  4. Type narrowing failure: variables have a wider type than needed but haven't been narrowed to the function's expected type
  5. Generic type inference problems where TypeScript cannot infer the correct type for a generic function
  6. Passing a more specific type where a more general type is expected (contravariance issues with function parameters)
  7. Using variables with inferred types that don't match the function parameter type
  8. Passing an object with optional properties that the function treats as required
  9. Strict null checks are enabled and you're passing a possibly-null value to a function that requires a non-null value

How to fix "Argument of type 'X' is not assignable to parameter of type 'Y'"

1Read the error message carefully to identify the exact types

The error message shows both the actual type (X) and the expected type (Y). Pay close attention to these types and understand what the difference is. For example, if it says "Argument of type 'string' is not assignable to parameter of type 'number'", you're passing a string where a number is expected.

typescript
// Error: Argument of type 'string' is not assignable to parameter of type 'number'
function add(a: number, b: number): number {
  return a + b;
}

add("5", 10); // WRONG - first argument is string, not number
2Check if you're passing the correct type

Look at the function definition and verify what type it expects. Then look at your function call and identify what type you're actually passing. In many cases, you simply need to convert the value to the correct type or use a different variable.

typescript
// CORRECT - convert string to number
const numberValue = parseInt("5", 10);
add(numberValue, 10); // number + number = OK

// OR use number literal
add(5, 10); // number + number = OK
3Ensure object properties match the expected type

If passing an object, verify that it has all required properties with the correct types. The error often occurs when an object is missing a required property or has a property with a different type than expected.

typescript
interface User {
  id: number;
  name: string;
  email: string;
}

function processUser(user: User): void {
  console.log(user.name);
}

// ERROR - missing required properties
processUser({ name: "John" });

// CORRECT - includes all required properties
processUser({ id: 1, name: "John", email: "john@example.com" });
4Handle null and undefined with proper type guards

If the error mentions "null" or "undefined", the function parameter may not accept these values. Use type guards or provide default values to ensure you're not passing null/undefined where not allowed.

typescript
function greet(name: string): void {
  console.log(`Hello, ${name}!`);
}

const userName: string | null = getUserName(); // might be null

// ERROR - might pass null
greet(userName);

// CORRECT - check for null first
if (userName !== null) {
  greet(userName);
}

// OR use default value
greet(userName ?? "Guest");
5Use type assertions when you're certain of the type

If you are absolutely certain the value is the correct type but TypeScript cannot infer it, you can use a type assertion with as. However, use this sparingly as it bypasses type safety.

typescript
const value: unknown = "hello";

// TypeScript doesn't know this is a string
// greet(value); // ERROR

// If you're certain it's a string, use 'as'
greet(value as string); // OK
6Fix generic function type parameters

For generic functions, make sure type parameters are correctly inferred or explicitly specified. Sometimes TypeScript cannot infer the correct type, so you need to specify it explicitly.

typescript
function identity<T>(value: T): T {
  return value;
}

// TypeScript might infer the wrong type
const result1 = identity({ name: "John" }); // inferred as { name: string }

// Explicitly specify the type to be sure
interface User {
  name: string;
  age: number;
}

const result2 = identity<User>({ name: "John", age: 30 }); // must match User fully
7Review strictNullChecks setting in tsconfig.json

If the error involves null or undefined, check your TypeScript configuration. The strictNullChecks compiler option (enabled by "strict": true) makes null and undefined stricter. Understanding this setting helps you know when to provide additional type guards.

json
{
  "compilerOptions": {
    "strict": true,        // Enables strictNullChecks
    "strictNullChecks": true   // null/undefined not allowed unless explicitly in type
  }
}

With strict mode, you must explicitly handle null/undefined:

typescript
function getName(user: { name?: string }): string {
  return user.name; // ERROR: name might be undefined
  // return user.name ?? ""; // CORRECT
}
8Check function overload declarations if using overloads

If a function has multiple overload signatures, ensure your argument matches one of the overload signatures. The error might indicate that your argument doesn't match any of the available overloads.

typescript
function process(value: string): string;
function process(value: number): number;
function process(value: string | number) {
  return value;
}

// These work:
process("hello");  // matches first overload
process(42);       // matches second overload

// This fails:
process(true);     // ERROR - boolean doesn't match string or number overload

Advanced notes

Type compatibility in TypeScript follows specific rules. For objects, TypeScript uses structural typing, meaning an object type is compatible if it has all the required properties with compatible types (even if it has extra properties, unless in strict mode). For function parameters specifically, TypeScript enforces stricter rules with contravariance when strictFunctionTypes is enabled, meaning a function expecting a more general type can safely receive a function with a more specific parameter type, but not vice versa. When working with unions (type X | type Y), ensure you narrow the type before passing to functions that only accept one specific type. For generic types, type inference sometimes fails with complex nested types; explicitly specifying type parameters can help. The strictNullChecks option significantly affects error messages: without it, null and undefined are assignable to almost any type, but with it (part of strict mode), you must explicitly handle these values. For callback functions, ensure parameter types align with what the function expects; many errors occur when passing callbacks with incompatible signatures to functions like Array.map, Array.filter, or event listeners.

Frequently asked questions

What does "TS2345" mean?
This error indicates a type mismatch between the argument you are passing to a function and the type of the parameter that function expects. TypeScript's type checker has determined that the type of your argument (X) is not compatible with the declared parameter type (Y). This is a fundamental type safety check that prevents passing invalid values to functions.
How do I fix "TS2345"?
This TypeScript error occurs when you pass an argument to a function whose type does not match the function parameter's expected type. It's a strict type checking error that prevents incompatible values from being passed to functions. Understanding type compatibility and using proper type annotations resolves this common issue.
What causes "TS2345"?
Passing a value of the wrong primitive type (string instead of number, boolean instead of string, etc.) Passing a value that might be null or undefined where those types are not accepted by the function Passing an object that is missing required properties or has extra properties the function doesn't expect
Is the "TS2345" error serious?
Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.

Official resources & further reading