There are multiple ways to convert a string to a number in TypeScript. If you already know JavaScript, you will find them familiar.
Convert A String To A Number In TypeScript
With + operator
As in JavaScript, the unary plus operator (+) can make an attempt at converting an operand into a number. This operand can be a string representation of floats and integers, as well as non-string values like null, true, and false.
This operator supports hexadecimal integers beside the decimal format (the 0x prefix has to be used for this). It can also convert negative numbers. However, it only supports representations of decimal values. When the operator fails to convert the string, it will return NaN.
This example shows you can use this operator in TypeScript. We use the typeof operator to verify the type of our values:
let str: string = "2022";
console.log(str);
console.log(typeof str);
let num: number = +str;
console.log(num);
console.log(typeof num);
Output:
2022
string
2022
number
Note: you can also use the unary negation operator (-), which will convert a non-number (such as a string) into a number and negate it. This operation is slower than the plus counterpart because it needs to carry out extra operations on the number.
let str: string = "2022";
let num: number = -str;
console.log(num);
console.log(typeof num);
Output:
-2022
number
With Number()
The Number() constructor can be used in JavaScript to create a Number object.
However, it is more commonly used as a function to convert values of other types, including strings, to primitive number values. The main difference is that the returned values don’t have properties or methods like Number objects.
You can use Number() as a function in TypeScript as well:
let str: string = "2022";
let num: number = Number(str);
console.log(num);
console.log(typeof num);
Output:
2022
number
With parseInt() And parseFloat()
Likewise, the parseInt() and parseFloat() of JavaScript can be invoked from your TypeScript code to parse a string argument and return an integer or a floating-point number.
Example:
let str1: string = "2022";
let num1: number = parseInt(str1);
console.log(num1);
console.log(typeof num1);
let str2: string = "3.14159";
let num2: number = parseFloat(str2);
console.log(num2);
console.log(typeof num2);
Output:
2022
number
3.14159
number
Remember that you can also add an argument to indicate the base of the number system that parseInt() will use to parse the string:
let str3: string = "0e1";
let num3: number = parseInt(str3, 16);
console.log(num3);
console.log(typeof num3);
Output:
225
number
In this case, 225 is the decimal value of 0e1 in the hexadecimal system (base 16).
Catching Errors
All the conversions above will return NaN when they fail to convert values of other types to numbers. You may need to test the returned value against NaN before using it in your code.
let str: string = "LearnShareIT";
let num = +str;
if (isNaN(num)) {
console.log('Invalid string');
}
Output:
Invalid string
In this example, we use the isNan() function to test whether a value is Not-a-Number and throw an error message if that is the case.
If you run into the error “Type ‘string | undefined’ is not assignable to type ‘string’.” , follow this guide for solutions.
Summary
You can use the unary plus operator or the Number(), parseInt(), and parseFloat() functions to convert a string to a number in TypeScript. They work in a similar way to operations in JavaScript.
Maybe you are interested:
- Using Number.toString() in TypeScript
- Concatenate a String and a Number in TypeScript
- Check if String is a Valid Number in TypeScript

My name is Robert. I have a degree in information technology and two years of expertise in software development. I’ve come to offer my understanding on programming languages. I hope you find my articles interesting.
Job: Developer
Name of the university: HUST
Major: IT
Programming Languages: Java, C#, C, Javascript, R, Typescript, ReactJs, Laravel, SQL, Python