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?
3
I think you should write : const y: ${number} = ${x};
Arnaud Flaesch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.