Special Sponsor:PromptBuilder— Fast, consistent prompt creation powered by 1,000+ expert templates.
Make your Product visible here.Contact Us

Home/TypeScript Errors/TS2345

TS2345

Argument of type 'X' is not assignable to parameter of type 'Y'

TypeScript TS2345 fires when you pass a value to a function whose type does not match the expected parameter type. The type system cannot prove the argument is safe to use in that position.

Why This Happens

You are passing a value of type A to a function that expects type B, and A is not assignable to B — for example, passing a string where a number is required, or a broader union type where a narrower type is expected.

Code That Triggers TS2345

// TS2345 examples
function greet(name: string): string {
  return "Hello, " + name;
}

greet(42);
// Error: Argument of type 'number' is not assignable to parameter of type 'string'

// Also common with unions
function process(id: number): void {}
const value: string | number = getValueFromAPI();
process(value);
// Error: Argument of type 'string | number' is not assignable to parameter of type 'number'

How to Fix TS2345

Option 1: Narrow the type before passing

const value: string | number = getValueFromAPI();
if (typeof value === "number") {
  process(value); // OK — narrowed to number
}

Option 2: Cast with 'as' when you're certain

process(value as number); // use only when you know it's safe

Option 3: Fix the function signature to accept the actual type

function process(id: number | string): void {
  const normalized = Number(id);
}

Frequently Asked Questions — TS2345

What does TS2345 mean?

TypeScript TS2345 means the value you're passing to a function doesn't match the type the function declared it would accept. TypeScript's type system prevents this at compile time to catch bugs before they reach runtime.

How do I fix TS2345 without 'any'?

Narrow the type with typeof, instanceof, or a type guard before passing the value. Alternatively, broaden the function's parameter type to accept the actual type you're passing.

Why does TS2345 happen with string | number unions?

A union type like string | number is wider than number alone. TypeScript won't let you pass a wider type where a narrower one is required — you must first narrow it (e.g., with typeof value === 'number') to guarantee safety.

Is it safe to use 'as' to suppress TS2345?

Only if you can guarantee at runtime that the value is the correct type. Using 'as' bypasses type checking — if the value is actually the wrong type, you'll get a runtime error instead of a compile-time one.

Can TS2345 happen with React props?

Yes. If you pass the wrong prop type to a React component — for example, a string where a number is expected — TypeScript reports TS2345. Fix by matching the prop type or updating the component's prop interface.

Convert JavaScript to TypeScript automatically

Paste your JS code and get type-annotated TypeScript — including fixes for common type errors — in seconds.

Try the converter →

Other TypeScript Errors

From the blog

View all →