Thursday, January 30, 2025
Google search engine
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

Recent Comments