typescripttemplate-strings

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?


Solution

  • I think you should write:

    const y: `${number}` = `${x}`;