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

TypeScriptBEGINNERMEDIUMUpdated Dec 8, 2025· 136 views

This error occurs when you pass a string argument to a function that expects a number parameter. TypeScript enforces strict type checking to prevent runtime errors. The fix involves converting the string to a number using parseInt(), parseFloat(), Number(), or the unary plus operator before passing it to the function.

Quick fix

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

// Method 1: Number() - Converts entire string to number
const str = "42";
const num = Number(str);  // 42

// Method 2: parseInt() - Extracts integer, ignores decimal part
const str2 = "42.99";
const int = parseInt(str2, 10);  // 42 (always include radix 10 for decimal)

// Method 3: parseFloat() - Preserves decimal values
const str3 = "42.99";
const float = parseFloat(str3);  // 42.99

// Method 4: Unary plus (+) - Quick coercion (less explicit)
const str4 = "42";
const num4 = +str4;  // 42
  1. Identify the source of the string value
  2. Choose the appropriate conversion method
  3. Convert the string before passing it to the function
  4. Add validation to handle conversion failures
typescript — error
// Method 1: Number() - Converts entire string to number
const str = "42";
const num = Number(str); // 42
// Method 2: parseInt() - Extracts integer, ignores decimal part
const str2 = "42.99";
const int = parseInt(str2, 10); // 42 (always include radix 10 for decimal)
// Method 3: parseFloat() - Preserves decimal values
const str3 = "42.99";
const float = parseFloat(str3); // 42.99
// Method 4: Unary plus (+) - Quick coercion (less explicit)
const str4 = "42";
const num4 = +str4; // 42
Argument of type 'string' is not assignable to parameter of type 'number'

What this error means

This TypeScript error indicates a type mismatch between what you are passing to a function and what the function expects. Specifically, you are trying to pass a string value to a parameter that has been declared as accepting only numbers. TypeScript is a statically-typed language that checks types at compile time rather than runtime. When TypeScript detects that you are passing incompatible types, it raises this error to prevent you from introducing bugs that would only manifest at runtime. This is one of the core strengths of TypeScript—catching type mismatches before your code runs. The error usually appears in three common scenarios: calling a function with the wrong type, assigning a value to a variable with an incompatible type, or passing arguments to constructors or methods that expect specific types.

Typical symptoms

  • TypeScript compiler error (TS2345) during compilation
  • IDE red squiggly underline on the offending function call or assignment
  • Build or lint process fails with the type mismatch error message
  • Code that works in JavaScript fails type checking in TypeScript
  • Function receiving unexpected string instead of numeric value at runtime (if you bypass type checking)

Common causes

  1. Passing a string argument to a function parameter typed as number without converting it first
  2. Retrieving a value from an API response, form input, or environment variable (which are typically strings) and passing it directly to a function expecting numbers
  3. Using template variables or string interpolation that produce strings instead of numbers
  4. Reading from user input, database queries, or file I/O without type conversion (these sources typically return strings)
  5. Calling parseInt() or parseFloat() incorrectly, or using the result in a way that suggests a number but the variable is still typed as string
  6. Using JSON data where numeric fields are stored as strings and not converted before use

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

1Identify the source of the string value

Look at the function call or assignment that triggered the error. Determine where the string is coming from. Common sources include:

- User input (form fields, text inputs)
- API responses or fetch() results
- Environment variables (process.env.*)
- Database query results
- File I/O operations
- JSON data
- URL parameters or query strings

For example, if you see processAge(userInput) where userInput is a string, that's the problem.

2Choose the appropriate conversion method

TypeScript provides several ways to convert strings to numbers. Pick the one that fits your use case:

typescript
// Method 1: Number() - Converts entire string to number
const str = "42";
const num = Number(str);  // 42

// Method 2: parseInt() - Extracts integer, ignores decimal part
const str2 = "42.99";
const int = parseInt(str2, 10);  // 42 (always include radix 10 for decimal)

// Method 3: parseFloat() - Preserves decimal values
const str3 = "42.99";
const float = parseFloat(str3);  // 42.99

// Method 4: Unary plus (+) - Quick coercion (less explicit)
const str4 = "42";
const num4 = +str4;  // 42
3Convert the string before passing it to the function

Apply the conversion at the call site, right before passing the value to the function:

typescript
// WRONG - causes TypeScript error
function calculatePrice(quantity: number): number {
  return quantity * 10;
}

const userInput = "5";  // From form field
calculatePrice(userInput);  // Error: string not assignable to number

// CORRECT - convert the string first
const quantity = parseInt(userInput, 10);
calculatePrice(quantity);  // OK

// Or inline the conversion
calculatePrice(parseInt(userInput, 10));  // OK
4Add validation to handle conversion failures

Use isNaN() to check if the conversion failed. Conversion functions like parseInt() and parseFloat() do not throw errors; they return NaN for invalid input.

typescript
const userInput = "hello";  // Not a valid number
const num = parseInt(userInput, 10);  // Returns NaN

if (isNaN(num)) {
  console.error("Invalid number provided:", userInput);
  // Handle the error appropriately
} else {
  processNumber(num);
}

// Alternative: Use Number() with additional validation
const num2 = Number(userInput);
if (Number.isNaN(num2) || !Number.isFinite(num2)) {
  console.error("Invalid number");
}
5Validate user input with type guards

For complex applications, use Zod, io-ts, or TypeScript type guards to validate input at the boundary (API endpoints, form handlers, etc.):

typescript
// Using Zod
import { z } from "zod";

const schema = z.object({
  quantity: z.string().transform(Number).pipe(z.number().positive())
});

const data = schema.parse({ quantity: "42" });  // Automatically converts and validates
processQuantity(data.quantity);  // Type is number

// Using a type guard
function isNumber(value: unknown): value is number {
  return typeof value === "number" && !isNaN(value);
}

const value = parseFloat(userInput);
if (isNumber(value)) {
  processNumber(value);  // Safe to use
}
6Use explicit function overloads for flexibility

If your function legitimately needs to accept both strings and numbers, use function overloads to make the intent clear and handle both cases:

typescript
// Define overloads
function processValue(value: number): void;
function processValue(value: string): void;

// Implementation
function processValue(value: number | string): void {
  const num = typeof value === "string" ? parseInt(value, 10) : value;
  
  if (isNaN(num)) {
    console.error("Invalid value");
    return;
  }
  
  console.log("Processing:", num);
}

processValue(42);     // OK
processValue("42");    // OK
processValue("hello"); // Runtime error (caught with isNaN)

Advanced notes

When working with JSON data, remember that JSON numbers become JavaScript numbers when parsed, but numbers in JSON strings (like "42") remain strings even after JSON.parse(). Always check your API response types. For form inputs, all values are strings by default in HTML, so conversion is essential. Be cautious with parseInt() on strings without a radix parameter—"08" with no radix may be parsed as octal in older browsers. Always specify radix 10 for decimal integers. When working with large numbers (beyond JavaScript's safe integer limit of 2^53 - 1), consider using BigInt instead. If you're processing CSV, XML, or other text formats, the string conversion problem is pervasive—validate early in your data pipeline. In TypeScript strict mode (recommended), implicit any coercions are disabled, making these type errors more visible and easier to catch during development rather than in production.

Frequently asked questions

What does "Argument of type 'string' is not assignable to parameter of type 'numb" mean?
This TypeScript error indicates a type mismatch between what you are passing to a function and what the function expects. Specifically, you are trying to pass a string value to a parameter that has been declared as accepting only numbers. TypeScript is a statically-typed language that checks types at compile time rather than runtime.
How do I fix "Argument of type 'string' is not assignable to parameter of type 'numb"?
This error occurs when you pass a string argument to a function that expects a number parameter. TypeScript enforces strict type checking to prevent runtime errors. The fix involves converting the string to a number using parseInt(), parseFloat(), Number(), or the unary plus operator before passing it to the function.
What causes "Argument of type 'string' is not assignable to parameter of type 'numb"?
Passing a string argument to a function parameter typed as number without converting it first Retrieving a value from an API response, form input, or environment variable (which are typically strings) and passing it directly to a function expecting numbers Using template variables or string interpolation that produce strings instead of numbers
Is the "Argument of type 'string' is not assignable to parameter of type 'numb" error serious?
It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.

Official resources & further reading