Saturday, October 25, 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
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS