0

I need to interpolate a variable which is a string, but this no working. Is there any way to interpolate this variable? Is it better to do the substitution directly?

for example:

time = duration.years();
dateTime = this.i18nService.getTranslation('past-date-years');
//dateTime value is = "${time} years ago"

console.log(`${dateTime}`); //not working

console.log(`${time} years ago`);//working
4
  • 3
    Define 'no working'. What output do you actually get from the first console.log() call? BTW, the answer to the question in the title is "yes". Commented Jul 5, 2017 at 12:49
  • are you sure there is something in the dateTime variable? try debugging or just console.log(dateTime) Commented Jul 5, 2017 at 13:08
  • yes I am sure. console.log(${dateTime}); = "${time} years ago" console.log(${time} years ago); = "3 years ago" Commented Jul 7, 2017 at 10:55
  • 1
    This is an old question, but I think what Roberto wanted to ask was "can you programmatically resolve a variable that might contain a template?". By using the backticks the compiler treats the hard-coded string as a template, but that works only on the hard-coded strings. What he needs is a function that resolves a given string, which I don't think exists in Typescript (at least not until December 2018 when I am writing this). Such a function would also have to be used with caution, since it would allow code injection. Commented Dec 13, 2018 at 8:57

2 Answers 2

2

Since your string interpolation syntax is correct, this would mean that either dateTime is null or an empty string. Please add the output of your logged strings to your question and/or any errors that are showing up in the console.

Based on your comments below, it seems that you have a string interpolation within a string interpolation where the first hasn't been evaluated.

The following (rudimentary) code works:

let time = "This is time";
let vari = `${time} | is what we got`;
let result = `${vari} | is this possible?`;
document.body.innerText = result;

...which will print the result with all the variables resolved, but that's because TypeScript knows how to break those lines apart and construct a normal concatenation from them.

It seems that in your instance, getTranslation() isn't resolving time, but is instead constructing an escaped string with the interpolation tokens in it. That needs to be resolved appropriately.

Since TypeScript compiles to different targets, and string interpolation is essentially TypeScript doing string concatenations behind the scenes, you're going to have to do some legwork to write the code and evaluate the resulting JavaScript to see if it gives valid output that will resolve the variables appropriately.

Head over to the TypeScript play, and use that to check the output. You'll have to plug in some of what the getTranslation() method is doing, and see if the output will generate appropriately.

Sign up to request clarification or add additional context in comments.

4 Comments

Hello, the results that were added in comments, dateTime value is = "${time} years ago" (With double quotation marks) the problem is that the value of the dateTime and bring double quotes to be a string so the interpolation does not work, thanks
What does the actual console say when you log to it (not just whether it's working or not)? When you do a step-through debug, what value does the dateTime variable have?
console.log(${dateTime}); = "${time} years ago" console.log(${time} years ago); = "3 years ago"
Updated the answer to reflect your comments.
1

It's nasty, but it works:

const str = "Hello ${word}"
const word = "world"
const result = eval("`" + str + "`")
console.log(result)

3 Comments

Eval should never be used, especially when talking about any given string. Allowing an input to execute arbitrary code is one of the worst vulnerabilities possible. If standard interpolation isn't working for them, there is a different issue that needs to be addressed.
In the case of TypeScript, that "other issue to be addressed" may well be that it's a fairly new language and they're still ironing out all the kinks. Eval is a high risk maneuver if you're working with user-supplied input, but that's not the only time dynamic code evaluation can be useful. It's naïve to assume no valid use-cases exist for it.
I do not know what strings they're trying to interpolate in reality, but eval should only be used with highly sanitized math. Using eval for interpolating strings is a mistake, and it's naive. The language is capable of basic string interpolation, eval is not needed. I interpolate strings in typescript regularly, it's not bare bones or incomplete.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.