0

In typescript, I want to assign a number variable to a string variable that only accepts numerical strings. I tried the following:

const x: number = 0;
const y: `${number}` = x.toString();

But instead of resolving to ${number} type, it instead resolves to string type, which of course is not assignable to ${number}, even though number variables should always resolve to numerical values when stringified.

Without type assertion, is there a way to do it?

2
  • 1
    I think you should write : const y: ${number} = ${x}; Commented May 2, 2024 at 13:12
  • @ArnaudFlaesch Yes, that's exactly right! Thanks! Can you post it as an answer please? Commented May 2, 2024 at 13:13

1 Answer 1

2

I think you should write:

const y: `${number}` = `${x}`;
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.