9

I'm using typescript, and is complaining when concatenating a string,

 const content = senderDisplay + ', '+ moment(timestamp).format('YY/MM/DD')+' at ' + moment(timestamp).format('h:mm A'); 

[tslint] Use a template literal instead of concatenating with a string literal. (prefer-template)

What is the template literal to fix this? cheers

2
  • MDN: Template literal. They're an alternate string literal, using ticks rather than quotes, and allow for embedding expressions. Commented Mar 12, 2018 at 2:12
  • tslint checks for style, you can look up the rules like prefer-template and you can see what is going on... Commented Mar 12, 2018 at 2:15

3 Answers 3

16

You can see the template literals in MDN, and these are the prefered style in ES6.

In your case it would become the following:

const content = `${senderDisplay}, ${moment(timestamp).format('YY/MM/DD')} at ${moment(timestamp).format('h:mm A')}`; 

Important differences:

  • Starts and ends with a backtick

  • Supports multiline strings

  • Expressions are interpolated with ${expression}

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

Comments

4
const content = `${senderDisplay}, ${moment(timestamp).format('YY/MM/DD')} at ${moment(timestamp).format('h:mm A')};` 

Check out https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals! They're awesome once you get the hang of it, string interpolation is much more readable than concatenating stuff together.

Comments

3

Use backticks and ${...}.

 const content = `${senderDisplay}, ${moment(timestamp).format('YY/MM/DD')} at ${moment(timestamp).format('h:mm A')}`; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.