TS2345
TypeScriptERRORNotableType SystemHIGH confidence

Argument of type is not assignable to parameter of type

What this means

The type of an argument passed to a function does not match the type of the parameter defined in the function signature.

Why it happens
  1. 1Passing a string to a function expecting a number.
  2. 2Providing an object that is missing a required property.
  3. 3Passing 'null' or 'undefined' to a function that does not explicitly allow it.
How to reproduce

A function expecting a number is called with a string.

trigger — this will error
trigger — this will error
function printValue(value: number) {
  console.log(value);
}
printValue("hello");

expected output

error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.

Fix 1

Pass an argument of the correct type

WHEN The function is being called with the wrong data.

Pass an argument of the correct type
function printValue(value: number) {
  console.log(value);
}
printValue(123);

Why this works

Providing a value that matches the parameter's type signature.

Fix 2

Adjust the function's parameter type

WHEN The function needs to handle different types.

Adjust the function's parameter type
function printValue(value: number | string) {
  console.log(value);
}
printValue("hello");

Why this works

Using a union type to allow multiple types for the parameter.

What not to do

Use 'as any' to cast the argument

This silences the compiler but undermines type safety, potentially causing runtime errors.

Sources
Official documentation ↗

microsoft/TypeScript src/compiler/diagnosticMessages.json

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All TypeScript errors