How to convert a string to a number in TypeScript? All methods and nuances
Hello everyone! I just started learning TypeScript and encountered a seemingly simple task. I have a variable that comes from a form or from localStorage, and it always has the string type. I need to perform mathematical operations with it, but TypeScript complains.
Here is an example of my code:
```typescript
const userAge: string = "25";
const nextYearAge = userAge + 1; // Result: "251", not 26!
console.log(nextYearAge); // Outputs "251" because string concatenation occurred
```
I understand that in JavaScript there are several ways to convert a string to a number: `parseInt()`, `parseFloat()`, `Number()`, unary plus `+`. But which one is better to use in TypeScript? And are there any TypeScript-specific methods?
I am interested in the following points:
1. Which method is