How to Fix TypeScript TS2345: Argument Not Assignable to Parameter
error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'. appears when you pass a string to a function that takes a number argument. The first 'string' is the type of the value you passed. The second 'number' is the type the parameter expects. If you change the value to match that type, the error goes away.
This article explains how to read the error and fix it when tsc stops with this error, or when VS Code shows a red squiggly line on a function call. In a test environment, I caused this error in four cases: a string passed to a number, a value that includes undefined, a string literal type, and an object. I include the messages exactly as they appeared. After each fix, I confirmed that the error disappeared, using both tsc and the TypeScript language server.
The test environment was Ubuntu 24.04, Node.js 22.23.4, npm 10.9.9, and TypeScript 7.0.2. I did not run an editor such as VS Code. Instead, I ran the TypeScript 7 language server directly (tsc --lsp --stdio) and checked the diagnostics it returned. I checked the terminal output by running the command in a pseudo-terminal. I did not test combinations with libraries such as React, and I did not test versions of TypeScript before 7.
For the tests, I ran npm init -y in an empty folder, added "type": "module" to package.json, installed TypeScript 7.0.2, and used the tsconfig.json generated by npx tsc --init without changes. This tsconfig.json has "strict": true.
The message when passing a string to a number parameter
I passed a variable containing "1200" in quotes to a function formatPrice, which formats an amount as 1,200円. The file is src/price.ts.
function formatPrice(yen: number): string {
return `${yen.toLocaleString("ja-JP")}円`;
}
const input = "1200";
console.log(formatPrice(input));
When I ran npx tsc --noEmit in the terminal, it displayed the following (colors removed):
src/price.ts:6:25 - error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
6 console.log(formatPrice(input));
~~~~~
Found 1 error in src/price.ts:6
When I sent the output somewhere other than the terminal (a pipe or a file), it used a one-line format, and the exit code was 1. I show the remaining errors in this short format too.
src/price.ts(6,25): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
In Japanese, the message means "a value of type string cannot be passed to a parameter of type number". In English, "Argument" refers to the value you pass when you call the function, and "parameter" refers to yen in the function declaration. The squiggly line is under the passed value input, not under the function name.
I also opened the same file in the TypeScript language server:
diagnostics (1):
6:25-6:30 severity=1 source=ts code=2345 Argument of type 'string' is not assignable to parameter of type 'number'.
hover 6:13: function formatPrice(yen: number): string
The range 6:25-6:30 covers the five characters of input. severity=1 means error, as defined in the Language Server Protocol specification. When I hovered over the function name formatPrice, the hover showed yen: number, which gives the parameter's type. When you see this error, hovering over the function name shows you the type the function expects.
JavaScript is still emitted even with the error
As described in the noEmitOnError documentation, the default for not emitting JavaScript when there are errors is false. When I ran the build with --outDir dist instead of --noEmit, TS2345 appeared and the exit code was 2, but dist/price.js was still generated. Running it displayed the following:
1200円
The comma is missing. Number's toLocaleString() returns a string formatted for the locale. Strings don't have their own toLocaleString(). In the Node.js test environment, calling it on a string used Object's toLocaleString(), and "1200".toLocaleString("ja-JP") returned 1200 unchanged. This does not cause a runtime error, so it is easy to miss if you build without the type check.
Convert to a number with Number() before passing
For a value that starts as a string, convert it to a number with Number() before passing it:
console.log(formatPrice(Number(input)));
npx tsc --noEmit printed nothing and exited with code 0. The language server returned 0 diagnostics. When I emitted and ran the code, it displayed 1,200円.
However, in the test environment, Number("abc") and Number("1,200") (with a comma) both returned NaN (a value meaning "not a number"). The type of NaN is also number, so the type check does not catch it. If you don't know what a value contains, check it with Number.isNaN() after converting. In the test environment, Number.isNaN(Number("abc")) returned true and Number.isNaN(Number("1200")) returned false.
Passing "string | undefined" to a string parameter
You also get this error when you pass the result of a function that returns undefined when no value is found, without checking it first. In src/color.ts, I passed the result of get() on a Map to a function that takes a string argument.
function toUpper(text: string): string {
return text.toUpperCase();
}
const colors = new Map([["red", "#ff0000"]]);
const code = colors.get("red");
console.log(toUpper(code));
src/color.ts(7,21): error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
Type 'undefined' is not assignable to type 'string'.
The message has two lines. The first line, 'string | undefined', means the type is either a string or undefined. The second line says that the undefined part is not assignable to string. The reason is indented below, so reading a long message from the bottom up shows you where the mismatch is.
"red" is in colors, but as the MDN documentation for Map.prototype.get() explains, get() returns undefined when the key is not found. The type declaration in TypeScript 7.0.2 (lib.es2015.collection.d.ts) also gives the return type as V | undefined. Hovering over code in the language server showed const code: string | undefined.
There are two ways to fix this. The first is to check with if that the value is not undefined before passing it:
const code = colors.get("red");
if (code !== undefined) {
console.log(toUpper(code));
}
As the handbook's explanation of narrowing with === and !== describes, a comparison such as !== makes TypeScript narrow the type inside the if block. Inside the if, hovering over code showed const code: string. Both tsc and the language server reported 0 errors. When I ran it, it displayed #FF0000.
The second way is to choose a fallback value with ?? when nothing is found. As the MDN explanation of ?? describes, ?? returns the right-hand value when the left-hand value is null or undefined:
const code = colors.get("red") ?? "#000000";
console.log(toUpper(code));
The type of code became string, and the error disappeared. Even when you're sure a value will always be found, writing an if or ?? lets you decide what happens when a key is mistyped.
Passing a variable to an argument that takes a string literal type
This error can also occur when the parameter type is a fixed set of strings, a string literal type such as "left" | "center" | "right". In src/align.ts, I passed a let variable and an object property.
type Align = "left" | "center" | "right";
function setAlign(align: Align) {
console.log(`align: ${align}`);
}
let align = "center";
setAlign(align);
const options = { align: "right" };
setAlign(options.align);
src/align.ts(8,10): error TS2345: Argument of type 'string' is not assignable to parameter of type 'Align'.
src/align.ts(11,10): error TS2345: Argument of type 'string' is not assignable to parameter of type 'Align'.
The values are "center" and "right", and both are strings included in Align. The errors occur because the type of the variable and the property is string. The hovers in the language server showed let align: string on line 7 and (property) align: string for options.align on line 11.
According to the handbook's explanation of literal types, a let variable can later hold a different string, so its type is string. Object properties work the same way because they can be reassigned. (See also the same page's explanation of how object property types are inferred.)
If you declare the variable with const and add as const to the object, the literal values become the types:
const align = "center";
setAlign(align);
const options = { align: "right" } as const;
setAlign(options.align);
The hovers now show const align: "center" and (property) align: "right", and the tsc errors disappeared. When I ran it, it displayed align: center and align: right.
If you need to keep let because you will change the value later, annotate the variable or object with the Align type. With this approach, npx tsc --noEmit also exited with code 0:
let align: Align = "center";
setAlign(align);
const options: { align: Align } = { align: "right" };
setAlign(options.align);
When you pass an object, the mismatched property appears at the bottom
When you pass an object, the message shows which property doesn't match on the lines below the main message. I passed an object whose id is a string to a function that takes a User type, where id is declared as a number. The file is src/user.ts.
type User = {
id: number;
name: string;
};
function saveUser(user: User) {
console.log(`saved: ${user.id} ${user.name}`);
}
const user = { id: "1", name: "Hana" };
saveUser(user);
src/user.ts(11,10): error TS2345: Argument of type '{ id: string; name: string; }' is not assignable to parameter of type 'User'.
Types of property 'id' are incompatible.
Type 'string' is not assignable to type 'number'.
The first line gives the type of the whole object. The second line says that the type of the id property does not match. The third line says that string cannot be assigned to number. The squiggly line is under the passed user. If you read down to the last line, you can see that id should be a number.
When I wrote the same object directly in the call instead of storing it in a variable, the error code and location changed:
saveUser({ id: "1", name: "Hana" });
src/user.ts:10:12 - error TS2322: Type 'string' is not assignable to type 'number'.
10 saveUser({ id: "1", name: "Hana" });
~~
src/user.ts:2:3 - The expected type comes from property 'id' which is declared here on type 'User'
2 id: number;
~~
In the test environment, this form produced TS2322 instead of TS2345, and the squiggly line covered only id, not the whole argument. The added line means that the expected type comes from the id property declared on User. The language server also reported one diagnostic with code=2322, covering id (10:12-10:14). So when you see TS2322 for an object written inside a call, the cause is still the type of the property you pass.
The fix is the same for both forms: change id to the number 1.
const user = { id: 1, name: "Hana" };
saveUser(user);
saveUser({ id: 1, name: "Hana" });
Both forms produced 0 errors from tsc and 0 diagnostics from the language server. When I emitted and ran the version that passes the variable, it displayed saved: 1 Hana.





