Rolando Rolando - 5 months ago 38
TypeScript Question

Best practice for using typescript const variable with class variables?

Using this code (within an angular2 service), I can see it works, I am guessing the backticks allow for evaluation of this.heroesUrl and as if it were in a template:

const url = `${this.heroesUrl}/${}`;
return this.http
.put(url, JSON.stringify(hero), {headers: this.headers})
.then(() => hero)

Question is, could I have just done this:

const url = this.heroesUrl + '/' +;

If so, is the former still preferred over the second given best practice?


The example uses es2015 template literals. In this case both of them are equivalent but template literals can be use to evaluate expressions inside strings.

For example if you'll like to add two variable inside a string:

  1. ES5: var sum = 'The sum is: ' + (a + b);
  2. ES2015: let sum =`The sum is ${ a + b}`