Converting a number to a string in TypeScript: how to do it correctly and without errors?
Hello everyone! I just started learning TypeScript and I ran into a problem with converting a number to a string. In regular JavaScript, I always used something like `String(123)` or `(123).toString()`, but in TypeScript I encountered unexpected type errors.
Here's an example of my code:
```typescript
const userId: number = 42;
const url: string = "/user/" + userId; // Error: Type 'number' is not assignable to type 'string'
```
I expected TypeScript to automatically convert the number to a string during concatenation, like JavaScript does, but I get a compilation error. I also tried:
```typescript
const userId: number = 42;
const url: string = "/user/" + String(userId); // Works, but is this correct?
```
I'm interested in several things:
1. Which method is considered
No answers yet.
Log in to leave an answer
Blogs
How to Write a REST API in PHP in 30 Minutes: Ready Code