Friday, September 19, 2025
HomeLanguagesJavascriptWhat are template literal types in Typescript ?

What are template literal types in Typescript ?

Template literal types in typescript are based on string literal types and may be expanded into many strings using unions. They have the same syntax as JavaScript’s template literal strings, but they’re utilized in type locations. When used with tangible literal types, a template literal concatenates the contents to create a new string literal type.

Syntax: The following is the syntax of creating template literals:

${...}

Note: ` ` is used instead of “” while creating template literals. 

Example 1: Concatenating string literals with strings.

In the below code we create a string literal and it is further concatenated with another string in “ ticks, using the ${} syntax. A new type is formed. 

Javascript

type coding = "coding";
type sentence = `i like ${coding}`;

let sentence1: sentence = "i like coding";
console.log(sentence1);
RELATED ARTICLES

Most Popular

Dominic
32301 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6666 POSTS0 COMMENTS
Nicole Veronica
11840 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11898 POSTS0 COMMENTS
Shaida Kate Naidoo
6781 POSTS0 COMMENTS
Ted Musemwa
7056 POSTS0 COMMENTS
Thapelo Manthata
6739 POSTS0 COMMENTS
Umr Jansen
6745 POSTS0 COMMENTS