This TypeScript error occurs when you pass a number value to a function parameter that expects a string. TypeScript is a strongly typed language and enforces type safety at compile time. This error prevents runtime type mismatches and is usually fixed by converting the number to a string using String(), toString(), or explicitly asserting the type.
TypeScript enforces strict type checking at compile time. When a function parameter is defined to accept a string type, TypeScript expects any value passed to that parameter to be a string. If you pass a number instead, TypeScript throws a compile-time error because the types do not match. This is a safety feature that prevents accidental type mismatches that could cause bugs at runtime. The error message tells you the source (a number) and the destination (expected string), making it straightforward to identify the type mismatch.
Look at the error message in your IDE or terminal output. It will show you the exact line number and function name. For example, the error might say line 42 in your component passes a number to greet(). Open that file and locate the offending function call.
The simplest and most reliable way to convert a number to a string is wrapping it with the String() constructor. This works for all number types including integers, floats, and special values like Infinity.
// WRONG - number passed to string parameter
function displayId(id: string) {
console.log("ID: " + id);
}
const userId = 42;
displayId(userId); // TS2345: Argument of type 'number' is not assignable to parameter of type 'string'
// CORRECT - convert number to string
displayId(String(userId)); // Works!Numbers have a built-in toString() method that converts them to strings. This is equivalent to String() but more explicit about the conversion.
const price = 99.99;
const message: string = price.toString(); // "99.99"
function logPrice(priceStr: string) {
console.log("Price: " + priceStr);
}
logPrice(price.toString()); // Works!Template literals (backticks) automatically convert numbers to strings when interpolated. This is often the cleanest approach when building strings.
const quantity = 5;
const item = "apples";
// Numbers in template literals are automatically converted
const result: string = `You have ${quantity} ${item}`; // "You have 5 apples"
function processOrder(orderDetails: string) {
// ...
}
processOrder(`Order: ${quantity} items`); // Works!Sometimes the real issue is overly restrictive typing. If a function logically should accept both numbers and strings, update the parameter type to use a union type. This is often better than converting every time.
// RESTRICTIVE - only accepts strings
function format(value: string): string {
return `Value: ${value}`;
}
format(42); // Error!
// BETTER - accepts both numbers and strings
function format(value: string | number): string {
return `Value: ${value}`;
}
format(42); // Works!
format("hello"); // Also works!Type assertions tell TypeScript to treat a value as a specific type. Use as string only when you're absolutely certain the value is actually a string at runtime. This bypasses type checking and can hide bugs.
// NOT RECOMMENDED - bypasses safety
const count = 10;
function log(message: string) {
console.log(message);
}
log(count as string); // Compiles but count is still a number at runtime!
// BETTER - actual conversion
log(String(count)); // Type safe and correctMake sure your TypeScript configuration is properly set up to catch these errors. Check your tsconfig.json and ensure strict mode is enabled. This catches more type issues early.
{
"compilerOptions": {
"strict": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitAny": true
}
}TypeScript's type system is structural, meaning it checks that values have the correct shape and type. The error "Argument of type 'number' is not assignable to parameter of type 'string'" is one of the most common type mismatches because developers sometimes assume automatic type coercion will happen (like in JavaScript). However, TypeScript enforces strict typing at compile time to prevent bugs. When working with APIs or external data sources, always validate and convert the types explicitly. For large codebases, consider using linting rules like @typescript-eslint/no-explicit-any to maintain type safety. If you frequently need to accept both strings and numbers, consider using generic types or overloaded function signatures for more expressive APIs.
Function expression requires a return type
Function expression requires a return type
Value of type 'string | undefined' is not iterable
How to fix "Value is not iterable" in TypeScript
Type 'undefined' is not assignable to type 'string'
How to fix "Type undefined is not assignable to type string" in TypeScript
Type narrowing from typeof check produces 'never'
How to fix "Type narrowing produces never" in TypeScript
Type parameter 'T' has conflicting constraints
How to fix "Type parameter has conflicting constraints" in TypeScript