How to Fix TS2322 “Type ‘string’ is not assignable to type ‘number'” in TypeScript
error TS2322: Type 'string' is not assignable to type 'number'. appears when you put a string value into a place declared as number. The first 'string' is the type of the value you are assigning, and the second 'number' is the type of the place it is assigned to. Write the value as a number, or convert it with Number() before assigning it, and the error goes away.
This article covers what to do when tsc stops with this error, or when your editor shows a red squiggle on a variable or a React prop. I triggered the error in my test environment in three places (a variable, a function's return value, and a props), and I included the messages as they appeared. I confirmed that the error goes away after the fix, both with tsc and with the TypeScript language server that editors use.
The test environment was Ubuntu 24.04, Node.js 22.23.4, npm 10.9.9, TypeScript 7.0.2, React 19.3.1, and @types/react 19.3.0. For the editor output, I did not use the editor itself such as VS Code. Instead I ran the TypeScript language server that editors use for diagnostics (tsc --lsp --stdio) directly. For rendering, I turned the output into an HTML string with react-dom/server and checked that. I did not test the browser screen, a build through a bundler such as Vite, or versions of TypeScript before 7.
- 1. The project I used for testing
- 2. The message for putting a string into a number variable
- 3. The same error can happen with variables without a type annotation and with function return values
- 4. When you pass a quoted number to a React props
- 5. For values that always arrive as strings, convert them with Number()
- 6. Passing a string to a function argument gives TS2345
The project I used for testing
I ran npm init -y in an empty folder and added "type": "module" to package.json. I installed TypeScript 7.0.2, React 19.3.1, and react-dom 19.3.1, along with their types (@types/react 19.3.0 and @types/react-dom 19.3.0), and created tsconfig.json with npx tsc --init. The generated tsconfig.json has "strict": true and "jsx": "react-jsx". To allow writing imports without file extensions, I changed "module" to "esnext" and added "moduleResolution": "bundler".
The message for putting a string into a number variable
Here is the shortest example. In src/count.ts, I put the quoted "3" into a variable declared as number.
let count: number = "3";
console.log(count);
I ran npx tsc --noEmit in the terminal, and it printed the following (colors removed):
src/count.ts:1:5 - error TS2322: Type 'string' is not assignable to type 'number'.
1 let count: number = "3";
~~~~~
Found 1 error in src/count.ts:1
The colored, underlined format comes from the pretty setting in tsconfig, which is enabled by default. In my test environment, when I piped the output, it changed to the one-line format below, and the exit code was 1. For the rest of the errors in this article, I show this shorter format.
$ npx tsc --noEmit
src/count.ts(1,5): error TS2322: Type 'string' is not assignable to type 'number'.
The message means "the string type cannot be assigned to the number type." "3" is a string even though its contents are digits, because it is wrapped in quotes. The : number in let count: number is the declaration that this variable accepts only numbers (TypeScript Handbook, explanation of type annotations on variables). The squiggle is drawn under the variable name count, not under the value "3".
I also opened the same file with the language server that editors use:
diagnostics (1):
1:5-1:10 severity=1 source=ts code=2322 Type 'string' is not assignable to type 'number'.
hover 1:5: let count: number
The diagnostic range (where the editor draws the squiggle) is 1:5-1:10, which is the five characters of count. severity=1 is the value that the Language Server Protocol specification uses for errors. The message text is the same as tsc. When you hover over the variable name, the hover shows let count: number, which is the variable's type.
When I changed "3" to the unquoted 3, npx tsc --noEmit printed nothing and exited with code 0, and the language server reported zero diagnostics.
let count: number = 3;
The same error can happen with variables without a type annotation and with function return values
You can get the same error even when you do not write : number. In src/total.ts, I started a variable with 0 and later assigned a string to it.
let total = 0;
total = "100";
console.log(total);
src/total.ts(2,1): error TS2322: Type 'string' is not assignable to type 'number'.
As the same section of the Handbook explains, a variable without a type annotation gets its type from the first value it is given (type inference). When I hovered over total on line 1 in the language server, it showed let total: number, so 0 made the type number. That is why the assignment on line 2 fails, and the squiggle appears under total on line 2 (2:1-2:6). The error line alone does not show a type declaration, so check the value on the line where the variable was created.
The same happens when you declare a function's return type as number and return a string (return type annotations). This is src/price.ts:
function getPrice(): number {
return "980";
}
console.log(getPrice());
src/price.ts(2,3): error TS2322: Type 'string' is not assignable to type 'number'.
The location (2,3) is the return line. The return type : number is on line 1, so from the error line, trace back to the line where the function is declared.
When you pass a quoted number to a React props
In React components, this error appears when you pass a number prop as a quoted string. I wrote src/Counter.tsx, which adds 1 to the received number and displays it.
type CounterProps = {
initial: number;
};
export function Counter({ initial }: CounterProps) {
return <p>次の数:{initial + 1}</p>;
}
In the calling side, src/App.tsx, I wrote initial="3" with quotes.
import { Counter } from "./Counter";
export function App() {
return <Counter initial="3" />;
}
$ npx tsc --noEmit
src/App.tsx(4,19): error TS2322: Type 'string' is not assignable to type 'number'.
The message text is the same as with variables, but the terminal output also shows where the expected type is declared (colors removed):
src/App.tsx:4:19 - error TS2322: Type 'string' is not assignable to type 'number'.
4 return <Counter initial="3" />;
~~~~~~~
src/Counter.tsx:2:3 - The expected type comes from property 'initial' which is declared here on type 'IntrinsicAttributes & CounterProps'
2 initial: number;
~~~~~~~
Found 1 error in src/App.tsx:4
The added lines mean that the expected type comes from initial, which is declared in CounterProps. The IntrinsicAttributes & part is not a type I wrote. It is declared in @types/react, and it contains only key. When you look for the props type, look at CounterProps after the &.
The language server also reported one diagnostic, with the squiggle on the attribute name initial (4:19-4:26). The same explanation that tsc adds comes along as related information (relatedInformation):
diagnostics (1):
4:19-4:26 severity=1 source=ts code=2322 Type 'string' is not assignable to type 'number'.
related src/Counter.tsx:2:3 The expected type comes from property 'initial' which is declared here on type 'IntrinsicAttributes & CounterProps'
hover 4:19: (property) initial: number
If you build with the error, the numbers join as text
Even with a type-check error, tsc still writes out JavaScript. (As the noEmitOnError documentation says, the default for not emitting on error is false.) When I removed --noEmit and wrote to dist, the same TS2322 appeared and the exit code was 2, but dist/App.js and dist/Counter.js were still created.
I rendered the emitted App to an HTML string with renderToStaticMarkup from react-dom/server, and got:
<p>次の数:31</p>
Because initial still held the string "3" when + 1 ran, it became string concatenation instead of addition, so it displays 31 instead of 4. This does not cause a runtime error, so it is easy to miss if you build without checking types.
Pass the number in curly braces to fix it
As the React documentation on curly braces in JSX explains, a quoted attribute is passed as a string. To pass a number, wrap it in curly braces, like initial={3}.
import { Counter } from "./Counter";
export function App() {
return <Counter initial={3} />;
}
npx tsc --noEmit exited with code 0, and the language server reported zero diagnostics. When I emitted and rendered it, the output was <p>次の数:4</p>.
If a props type is number, pass the value in curly braces, like initial={3}. initial="3" is a string, even if its contents are digits.
For values that always arrive as strings, convert them with Number()
Some values, such as URL query strings or form input values, always arrive as strings, even when they contain digits. For example, URLSearchParams.get() returns either a string or null. In src/page.ts, I put that value into a variable of type number.
const params = new URLSearchParams("?page=2");
const page: number = params.get("page") ?? "1";
console.log(page + 1);
src/page.ts(2,7): error TS2322: Type 'string' is not assignable to type 'number'.
In this case, you cannot fix it by removing the quotes. Convert the value to a number with Number() before assigning it:
const params = new URLSearchParams("?page=2");
const page: number = Number(params.get("page") ?? "1");
console.log(page + 1);
npx tsc --noEmit exited with code 0. When I emitted and ran it with Node.js, it printed 3.
However, the result of Number() is not always a usable number. A string that cannot be read as a number becomes NaN (a value meaning "not a number"). Its type is still number, so the type checker does not catch it. In my test environment, I converted several strings:
$ node -e 'console.log(Number("2"), Number(""), Number("abc"), Number(" 12 "), Number("1,000"))'
2 0 NaN 12 NaN
An empty string becomes 0, leading and trailing spaces are ignored, and "abc" and "1,000" (with a comma) become NaN. When you do not know what a value will contain, such as an input field's value, you need separate code after the conversion to check it, for example with Number.isNaN().
Passing a string to a function argument gives TS2345
There is a related error with a different number. Here I passed a string to a function that takes a number argument:
function setCount(value: number) {
console.log(value);
}
setCount("5");
src/setter.ts(4,10): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
Assignments (to variables, return values, and props) give TS2322, while function arguments give TS2345. The update function from React's useState is also a function, so if you pass an input field's value directly, you get this form of the error:
import { useState } from "react";
export function PageInput() {
const [page, setPage] = useState(1);
return <input value={page} onChange={(e) => setPage(e.target.value)} />;
}
src/PageInput.tsx(5,55): error TS2345: Argument of type 'string' is not assignable to parameter of type 'SetStateAction<number>'.
SetStateAction<number> is the type meaning either a number, or a function that receives the previous value and returns a number. When I converted the value before passing it, as in setPage(Number(e.target.value)), the error went away. As with TS2322, the first type is the type of the value you passed, and the second type is the type the argument requires.





