Exactly like in JavaScript, you can use the parseInt or parseFloat functions, or simply use the unary + operator:
var x = "32";
var y: number = +x;
All of the mentioned techniques will have correct typing and will correctly parse simple decimal integer strings like "123", but will behave differently for various other, possibly expected, cases (like "123.45") and corner cases (like null).
Table taken from this answer
Exactly like in JavaScript, you can use the parseInt or parseFloat functions, or simply use the unary + operator:
var x = "32";
var y: number = +x;
All of the mentioned techniques will have correct typing and will correctly parse simple decimal integer strings like "123", but will behave differently for various other, possibly expected, cases (like "123.45") and corner cases (like null).
Table taken from this answer
The TypeScript way to do this would be:
Number('1234') // 1234
Number('9BX9') // NaN
as answered here: https://stackoverflow.com/a/23440948/2083492
Easiest Ways to Convert Strings to Numbers in TypeScript [Pie Chart]
Is there a way to avoid explicitly casting object keys to numbers?
How to define a function that has any number of string or number parameters
Why "Operator '+' cannot be applied to types 'string | number'"?
Videos
Converting strings to numbers in TypeScript is easy! This pie chart breaks down the most popular methods: Unary Operator, Number(), and parseInt/parseFloat.
Which one do you prefer?