How to fix Argument of type 'number' is not assignable to parameter of type 'string' in TypeScript

TypeScriptBEGINNERMEDIUMUpdated Dec 8, 2025ยท 117 views

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.

Quick fix

Fixes Argument of type 'number' is not assignable to parameter of type 'string'

// 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!
  1. Identify the function call causing the error
  2. Convert the number to a string using String()
  3. Use toString() method as an alternative
  4. Use template literals for string interpolation
typescript โ€” error
// 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!
Argument of type 'number' is not assignable to parameter of type 'string'

What this error means

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.

Typical symptoms

  • Red squiggly line in your IDE under the problematic function call
  • Compiler error in the terminal when running `tsc` or `npm run build`
  • Development server fails to start due to TypeScript compilation errors
  • Your code editor (VS Code, WebStorm, etc.) shows an inline error tooltip
  • Build process exits with "TS2345" or similar error code

Common causes

  1. Passing a number directly to a function that expects a string parameter without any conversion
  2. Receiving a number from JSON parsing, API responses, or user input that should be converted to a string
  3. Accessing a numeric property or variable and passing it to a string-expecting function
  4. Using array indices, numbers from loops, or numeric calculations as function arguments
  5. Importing data typed as number from a library and using it where string is required
  6. Destructuring or spreading objects that contain numeric values into string-expecting contexts

How to fix "Argument of type 'number' is not assignable to parameter of type 'string'"

1Identify the function call causing the error

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.

2Convert the number to a string using String()

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.

typescript
// 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!
3Use toString() method as an alternative

Numbers have a built-in toString() method that converts them to strings. This is equivalent to String() but more explicit about the conversion.

typescript
const price = 99.99;
const message: string = price.toString();  // "99.99"

function logPrice(priceStr: string) {
  console.log("Price: " + priceStr);
}

logPrice(price.toString());  // Works!
4Use template literals for string interpolation

Template literals (backticks) automatically convert numbers to strings when interpolated. This is often the cleanest approach when building strings.

typescript
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!
5Check function parameter types and consider using union types

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.

typescript
// 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!
6Use type assertion as a last resort

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.

typescript
// 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 correct
7Enable strict TypeScript checking in tsconfig.json

Make 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.

json
{
  "compilerOptions": {
    "strict": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "noImplicitAny": true
  }
}

Advanced notes

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.

Frequently asked questions

What does "Argument of type 'number' is not assignable to parameter of type 'stri" mean?
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.
How do I fix "Argument of type 'number' is not assignable to parameter of type 'stri"?
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.
What causes "Argument of type 'number' is not assignable to parameter of type 'stri"?
Passing a number directly to a function that expects a string parameter without any conversion Receiving a number from JSON parsing, API responses, or user input that should be converted to a string Accessing a numeric property or variable and passing it to a string-expecting function
Is the "Argument of type 'number' is not assignable to parameter of type 'stri" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading