Using Template Literals in TypeScript

24 Jun 2023 Balmiki Mandal 0 Typescript

Template Literals in TypeScript

Template literals in TypeScript are strings that include variables that can be easily substituted into the string. Templated literals are written using backticks (`) rather than single or double quotes. Template literals provide a simple and convenient way to integrate variables into a formatted string. In this article, we'll take a look at how to use template literals in TypeScript.

What are Template Literals?

Template literals are essentially syntactic sugar for making string-based operations simpler. They allow you to use string interpolation — a feature that allows you to easily incorporate variables and placeholders into strings. For example, a standard string would look like this:

let firstName = "John";
let lastName = "Doe";
let fullName = firstName + " " + lastName;

console.log(fullName); // John Doe

Using a template literal, the same operation can be performed much more quickly and simply:

let firstName = "John";
let lastName = "Doe";
let fullName = `${firstName} ${lastName}`;

console.log(fullName); // John Doe

As you can see, template literals are significantly shorter and easier to read. They also allow you to add line breaks and extra spaces to your strings without having to escape the characters. This makes them perfect for formatting multiline messages or complex output structures.

Using Template Literals in TypeScript

Template literals can be used in TypeScript for any number of scenarios. The most common use is for string interpolation, which allows you to incorporate variables into your strings. To use string interpolation with a template literal, you'll need to wrap the variable in curly braces preceded by a dollar sign (e.g. ${variableName}).

Template literals can also be used to create multiline strings. This is especially useful if you need to format contents such as HTML or SQL within a string. For example:

let htmlString = `<html>
  <head>
    <title>My Title</title>
  </head>
  <body>
    Hello World!
  </body>
</html>`;

When storing values in an object, template literals can make the process much simpler. Instead of having to manually concatenate multiple values, you can simply pass the values directly into the object:

let person = {
  firstName: `John`,
  lastName: `Doe`,
  age: 25
};

Finally, template literals can be used to perform advanced operations such as conditionals and loops. This is done via the $ tag, which allows you to pass expressions into the template literal. For example:

let greeting = `Hello,${person.firstName} ${
  person.age > 18 ? `adult` : `minor`
}!`;

In this example, the expression person.age > 18 ? `adult` : `minor` will evaluate to either adult or minor, depending on the value of the age property. The result is then inserted into the string.

Conclusion

Template literals are a powerful tool for quickly integrating variables into strings, formatting multiline strings, and performing advanced operations such as conditionals and loops. They can make code significantly shorter and easier to read, and are highly recommended for any project that uses TypeScript.

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.