Given a string representation of a number, how can I convert it to number type in TypeScript?
var numberString: string = "1234";
var numberValue: number = /* what should I do with `numberString`? */;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Exactly like in JavaScript, you can use the
parseIntorparseFloatfunctions, or simply use the unary+operator: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 (likenull).Table taken from this answer