Ad Space — Top Banner

TS2345

TypeScript TypeScript

Severity: Minor

What it means

TS2345 means you passed an argument of the wrong type to a function.
The function expects one type but you gave it something different.
Check the function's parameter types and make sure your argument matches.

Affected Models

  • TypeScript 4.x
  • TypeScript 5.x
  • VS Code
  • WebStorm

Common Causes

  • Passing a string where a number is expected (or vice versa)
  • Passing null or undefined to a parameter that does not accept them
  • Object missing required properties that the function expects
  • Using a union type that is wider than the parameter accepts
  • Array of wrong element type

How to Fix It

  1. Read the error message — it tells you both the expected and actual types.

    TS2345 says: Argument of type 'X' is not assignable to parameter of type 'Y'.
    X is what you passed.
    Y is what the function expects.

  2. Convert the value to the correct type.

    String to number: Number(value) or parseInt(value).
    Number to string: String(value) or value.toString().
    Use type assertions (as Type) only when you are certain.

  3. Add null checks if the parameter does not accept null/undefined.

    if (value !== null && value !== undefined) { doSomething(value); }
    Or use the non-null assertion operator (value!) if you are certain it is not null.

  4. Check if the function signature needs to be updated.

    Sometimes the function should accept more types than it currently does.
    Adding a union type to the parameter may be the correct fix.

Frequently Asked Questions

What is the difference between TS2345 and TS2322?

TS2345 is about function arguments — you passed the wrong type to a function.
TS2322 is about assignments — you assigned the wrong type to a variable.
Both are type mismatch errors but in different contexts.

Should I use type assertions (as) to fix this?

Only if you are absolutely certain the value is the correct type at runtime.
Type assertions bypass the type checker — if you are wrong, you get runtime errors.
Prefer actual type conversion or null checking.