TS2345

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

๐Ÿ“š Difficulty: Intermediate โฑ๏ธ 6 min read ๐Ÿ”„ Updated: Dec 2024

What is TS2345?

TS2345 occurs when you call a function with an argument that doesn't match the expected parameter type. This error is similar to TS2322, but specifically applies to function calls.

// Example of TS2345 error
function greet(name: string): void {
    console.log(`Hello, ${name}!`);
}

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

Common Causes

1. Wrong Primitive Type

function calculateAge(birthYear: number): number {
    return 2024 - birthYear;
}

// โŒ Wrong - passing string instead of number
calculateAge("1990"); // Error!

// โœ… Correct
calculateAge(1990);

2. Missing Properties in Object Argument

interface User {
    id: number;
    name: string;
    email: string;
}

function createUser(user: User): void {
    console.log(user);
}

// โŒ Missing 'email' property
createUser({ id: 1, name: "John" }); // Error!

// โœ… All properties included
createUser({ id: 1, name: "John", email: "john@example.com" });

3. Null or Undefined Issues

function processName(name: string): string {
    return name.toUpperCase();
}

const userName: string | null = getUserName();

// โŒ userName might be null
processName(userName); // Error!

// โœ… Check for null first
if (userName !== null) {
    processName(userName);
}

// โœ… Or provide default
processName(userName ?? "Guest");

4. Array Type Mismatch

function sumNumbers(numbers: number[]): number {
    return numbers.reduce((a, b) => a + b, 0);
}

// โŒ Mixed array
sumNumbers([1, 2, "3", 4]); // Error!

// โœ… Correct
sumNumbers([1, 2, 3, 4]);

5. Generic Type Mismatch

function getFirst<T>(arr: T[]): T {
    return arr[0];
}

const result: string = getFirst([1, 2, 3]); // Error!
// Type 'number' is not assignable to type 'string'

// โœ… Correct
const result: number = getFirst([1, 2, 3]);
// Or
const result: string = getFirst(["a", "b", "c"]);

Solutions

1. Fix the Argument Type

// Convert or provide correct type
const yearStr = "1990";
calculateAge(parseInt(yearStr, 10)); // Convert to number

2. Update Function Parameter Type

// Accept multiple types with union
function processId(id: string | number): void {
    console.log(id);
}

processId("abc"); // OK
processId(123);    // OK

3. Use Optional Properties

interface User {
    id: number;
    name: string;
    email?: string; // Optional
}

function createUser(user: User): void { }

createUser({ id: 1, name: "John" }); // OK now

4. Handle Null/Undefined

// Non-null assertion (use carefully)
processName(userName!); // Assert non-null

// Default value
processName(userName || "Default");

// Nullish coalescing
processName(userName ?? "Default");

5. Use Partial for Optional Properties

interface User {
    id: number;
    name: string;
    email: string;
}

// All properties become optional
function updateUser(updates: Partial<User>): void { }

updateUser({ name: "New Name" }); // OK

๐Ÿ’ก Pro Tip

When working with API responses, use type guards or validation libraries like Zod to ensure data matches expected types before passing to functions.

Real-World Examples

React Event Handlers

// โŒ Wrong event type
const handleClick = (e: MouseEvent) => { };
<button onClick={handleClick}>Click</button> // Error!

// โœ… Use React's event type
const handleClick = (e: React.MouseEvent<HTMLButtonElement>) => { };
<button onClick={handleClick}>Click</button> // OK

Array Methods

const numbers = [1, 2, 3, 4, 5];

// โŒ Callback returns wrong type
const doubled: number[] = numbers.map(n => n.toString()); // Error!

// โœ… Correct
const doubled: number[] = numbers.map(n => n * 2);
const strings: string[] = numbers.map(n => n.toString());

Promise/Async Functions

async function fetchUser(id: number): Promise<User> {
    // ...
}

// โŒ Passing string instead of number
fetchUser("123"); // Error!

// โœ… Correct
fetchUser(123);
fetchUser(parseInt(userId, 10));

โš ๏ธ Common Mistake

Don't use type assertions (as Type) to silence TS2345 errors. This bypasses type checking and can cause runtime errors. Fix the actual type mismatch instead.

Summary

Still Confused by Your Error?

Paste your TypeScript error into our analyzer for instant, detailed explanations.

Try Error Analyzer โ†’