How to check if a string is a number in TypeScript?
Hi everyone! I just started learning TypeScript and ran into this problem. I have a function that takes a string, and I need to determine if this string contains only digits (i.e., whether it is a numeric representation).
I tried using typeof, but it always returns 'string' for strings. I also tried using Number() or parseInt(), but they behave strangely: for example, Number("123abc") returns NaN, while parseInt("123abc") returns 123. That's not exactly what I need.
Here's an example of the code I'm trying to write:
```typescript
function isNumericString(value: string): boolean {
// How to check correctly?
return typeof value === 'number'; // Doesn't work, value is always a string
}
// Examples:
console.log(isNumericString("123")); // Should be
No answers yet.
Log in to leave an answer