Skip to content

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

This error occurs when you pass an argument to a function that doesn’t match the expected parameter type.


function greet(name: string) {
console.log(`Hello, ${name}!`);
}
const userName: string | undefined = getUserName();
greet(userName);
// ❌ Argument of type 'string | undefined' is not assignable to parameter of type 'string'

const userName = getUserName();
if (userName !== undefined) {
greet(userName); // ✅
}
greet(userName ?? "Guest"); // ✅
function greet(name: string | undefined) {
console.log(`Hello, ${name ?? "Guest"}!`);
}
greet(userName); // ✅

function setUserId(id: number) { /* ... */ }
interface Props {
userId?: number;
}
function Component({ userId }: Props) {
setUserId(userId);
// ❌ Argument of type 'number | undefined' is not assignable
// ✅ Fix:
if (userId !== undefined) {
setUserId(userId);
}
}

  • Always check the function signature
  • Use type narrowing or defaults before calling
  • Consider making function parameters more flexible