ErrorOracle
typescript

TS2345: Argument of type 'string' is not assignable to para…

Error message

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

What broke

The TypeScript compiler is indicating that a function or method was called with a string argument, but it requires a number. This mismatch leads to a type error.

Why it broke

TypeScript enforces type safety, and when a function is defined to accept a specific type (like number), passing a different type (like string) violates this contract. This is a common issue when types are not correctly aligned.

How to fix

To resolve this error, ensure that the argument being passed is of the correct type. If the value is a string that represents a number, convert it to a number using `Number()` or `parseInt()`/`parseFloat()` as appropriate.

Code fix

myFunction(Number(myString));

Explained by ErrorOracle

Prevention tip

If the value is a string that represents a number, convert it to a number using `Number()` or `parseInt()`/`parseFloat()` as appropriate.

Was this helpful?

AI-generated explanation. Always verify fixes in your codebase.