Type Transformations Workshop (6 exercises)
solution

Template Literal with Strings

The template literal syntax in TypeScript is similar to the JavaScript syntax, except you can put types inside of it.

Here's how we would format strings that begin with a slash:

Experiment with @ts-expect-error

Note that we are using the @ts-expect-error comments for some of the strings. These tell TypeScript that we expect the next line to have an error:

If we remove these comments, we will get an error:

Prepending the forward slash to these strings, we no longer get an error:

Template literal types let us be really specific with the types of strings that you can pass in. They also have several other useful scenarios that we'll see throughout this section.