manuelBetancurt
manuelBetancurt

Reputation: 16168

typescript asking for template literal

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

Upvotes: 9

Views: 8053

Answers (3)

Isac
Isac

Reputation: 1874

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}

Upvotes: 16

Dom
Dom

Reputation: 1816

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.

Upvotes: 4

Daniel A. White
Daniel A. White

Reputation: 191037

Use backticks and ${...}.

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

Upvotes: 3

Related Questions