Template Strings, String interpolation & multi-line Typescript

Template strings (or Template literals ) in Typescript are multi-line string literals that allow string interpolation. String interpolation allows us to include embedded expressions as part of the string. Template strings allows us to create multi-line strings, basic string formatting & tagged templates. They are part of ES2016/ES6 specification. In this tutorial, we will learn the Template strings (or Literals) syntax and how to use it in Multiline Strings & string interpolation. We will also learn how to nest expressions and how to Escaping template literals using the backslash (\)

What is Template Strings

Template literals are literals delimited with backticks ` instead of single quote or double quotes.

The following is the syntax of template literals

  1. We must enclose them in template literal syntax or back-tick.
  2. The expression must be specified inside dollar sign and curly braces (${expression})
  3. You must enable the "target": "es6", or "target": "es2015", in the tsconfig.json file
  4. Template literals always produce strings

The following is an example of Template string

What makes them powerful is you can include a embedded expression within them. We must include the expression inside a dollar sign and curly braces (${expression}). In the following example ${name} is an expression. The Typescript evaluates it and replaces its value in the its original position.

Multiline strings

The following is an example of creating a multiline string. Just hit enter and continue in the next line. The \n character is automatically gets inserted in the string.

Example

The multiline strings without back-quote are created by inserting a \n newline character (LF).

As we said earlier, the /n (LF) character is inserted when you use the template strings. Hence the following code is true.

String Interpolation

String interpolation (expression Interpolation) is the process of evaluating a string literal containing one or more expressions. It evaluates the expression and coerces the result into a string. The result is then is replaced in the original string.

Example

In the following example, $(palyerName) is the expression. It is evaluated first and the resulting value “Sachin Tendulkar” is replaced at its place in the final string.

Multiple Expressions

Using Expressions

The following example shows that you can use any expressions inside the template string. It uses the arithmetic expression ${a+b}

The following example uses the ternary conditional operator(?) in the template string.

Nesting Expressions

You can nest expressions

Escaping in template strings

The backslash \ is used for escaping inside template literals. It enables you to mention backticks and ${ inside template literals:

Tagged Templates

Another use of the Template string is in Tagged Templates. We can tag a function to a template string. This gives an opportunity to parse the template literals and control how it gets converts into a string.

You can read more about it from Tagged Templates in the next tutorial.

2 thoughts on “Template Strings, String interpolation & multi-line Typescript”

  1. Request regarding angular typescript.
    I have created a dynamic template with is supposed to be updated at run-time as its button text and some labels. but my model gets updated , view is not being changed on the map marker where the dynamic template shown

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top