Ad Space — Top Banner

TS2345

TypeScript Programming Language

Severity: Minor

What Does This Error Mean?

TypeScript error TS2345 means you passed a value to a function, but the type of that value does not match what the function expects. For example, passing a string to a function that only accepts numbers, or passing an object that is missing required fields. TypeScript catches this before your code runs so you avoid passing bad data to functions. Fix it by converting the value to the correct type, updating the function signature, or narrowing the type first.

Affected Models

  • TypeScript 1.0 and later
  • All TypeScript versions
  • tsc compiler, ts-node, webpack, Vite, Next.js

Common Causes

  • Passing a value of type string where number is required, or similar basic type mismatches
  • Passing an object that is missing one or more required properties defined in the parameter type
  • Passing null or undefined to a function parameter that does not accept those values
  • Passing a wider type (like string) where a narrower literal type ('left' | 'right') is required
  • TypeScript inferred the argument type as something broader than what the function accepts

How to Fix It

  1. Read the full error message. It will say something like 'Argument of type string is not assignable to parameter of type number'. Note both types.

    VS Code shows this inline as a red squiggle. Hover over it to read the full message without running tsc.

  2. If you need to convert between basic types, do so explicitly. To convert string to number: Number(myString) or parseInt(myString, 10).

    TypeScript does not convert types automatically. Every conversion must be intentional and explicit.

  3. If you are passing an object, check that it has all the required properties that the function's parameter type demands.

    Add any missing properties to your object. If a property is optional in the function but required in your object, check the interface definition.

  4. If the error is about null or undefined, add a null check before the call: if (myValue !== null) { myFunction(myValue); }

    Or use the non-null assertion operator (!) if you are certain the value is not null: myFunction(myValue!). Use ! sparingly.

  5. If you are passing a string variable where a string literal union is needed ('left' | 'right'), use 'as const' when declaring the variable, or cast it: myFunc(myVar as 'left' | 'right').

    TypeScript infers string variables as type string, not as a specific literal. Using 'as const' or a cast narrows it.

When to Call a Professional

TS2345 is a compile-time error. The error message tells you what type was passed and what type was expected. Compare the two carefully — the difference is usually obvious once you see both types side by side.

Frequently Asked Questions

What is the difference between TS2345 and TS2322?

TS2345 happens when you pass a wrong type as a function argument. TS2322 happens when you assign a wrong type to a variable. They are the same kind of type mismatch — one is at a function call site, the other is at an assignment.

The types look the same to me — why does TypeScript still complain?

TypeScript compares types structurally — it checks every field, including optional ones and nested types. A small difference deep in a nested type can cause TS2345. Expand the error in VS Code to see the full diff. Often the issue is a missing field, a null vs undefined distinction, or a readonly vs mutable difference.

Should I use 'as' to cast and silence the error?

Only as a last resort. Casting with 'as' tells TypeScript to trust you and stops the error, but it can hide real bugs. A better approach is to fix the actual type mismatch. If you are genuinely sure the types are compatible, a narrow cast (value as SpecificType) is better than casting to any.