How can I convert a TypeScript string to a number?

How can I convert a string to a number in TypeScript?

Given a string representation of a number, such as:

var numberString: string = “1234”;

What is the correct way to convert numberString to a number type in TypeScript? I am looking for a method to convert a typescript string to number.

The Number constructor can be used to convert a string to a number.

var numberString: string = "1234";
var numberValue: number = Number(numberString);
console.log(numberValue); // Outputs: 1234

The parseInt function converts a string to an integer. You can specify the radix (base) for the conversion.

var numberString: string = "1234";
var numberValue: number = parseInt(numberString, 10); // 10 is the radix for decimal numbers
console.log(numberValue); // Outputs: 1234

The parseFloat function converts a string to a floating-point number. This is useful if the string might contain decimal values.

var numberString: string = "1234.56";
var numberValue: number = parseFloat(numberString);
console.log(numberValue); // Outputs: 1234.56

Each of the methods will convert a typescript string to number and is suitable for different scenarios depending on whether you need an integer or a floating-point number.