Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't assign numerical value to numerical template string in typescript

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I think you should write:

const y: `${number}` = `${x}`;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading