What are template literal types in Typescript ? Last Updated : 24 Jan, 2025 Comments Improve Suggest changes Like Article Like Report Template literal types in TypeScript allow the construction of new string literal types by combining existing string literal types using template literal syntax.They enable the creation of complex string patterns by embedding unions and other literal types within template literals.This feature enhances type safety by allowing developers to define and enforce specific string formats at the type level. JavaScript type Size = "small" | "medium" | "large"; type SizeMessage = `The selected size is ${Size}.`; let message: SizeMessage; message = "The selected size is small."; // Valid message = "The selected size is extra-large."; // Error Size is a union type representing possible sizes.SizeMessage is a template literal type that constructs specific string patterns incorporating each Size value.The variable message can only be assigned strings that match the SizeMessage pattern.Output:Type '"The selected size is extra-large."' is not assignable to type 'SizeMessage'.More Example of template literal types in Typescript Defining Paths Using TypeScript Literals JavaScript type ApiEndpoints = "users" | "posts" | "comments"; type ApiPath = `/api/${ApiEndpoints}`; const userPath: ApiPath = "/api/users"; const invalidPath: ApiPath = "/api/unknown"; ApiEndpoints is a union type representing possible API endpoint names.ApiPath is a template literal type that dynamically constructs string patterns prefixed with /api/ followed by one of the ApiEndpoints.userPath is valid because it matches the constructed pattern, while invalidPath throws an error.Output:Type '"/api/unknown"' is not assignable to type 'ApiPath'.Formatting Messages Using Template Literals JavaScript type Status = "success" | "error" | "loading"; type StatusMessage = `The operation is ${Status}.`; const successMessage: StatusMessage = "The operation is success."; const invalidMessage: StatusMessage = "The operation is pending."; Status is a union type representing possible operation statuses.StatusMessage constructs string patterns to describe the status of an operation.successMessage is valid because it matches the pattern, but invalidMessage throws an error as "pending" is not part of Status.Output:Type '"The operation is pending."' is not assignable to type 'StatusMessage'. Comment More info L lizzywizzy366 Follow Improve Article Tags : TypeScript Geeks-Premier-League-2022 JavaScript-Questions Explore TypeScript Tutorial 8 min read TypeScript BasicsIntroduction to TypeScript 3 min read Difference between TypeScript and JavaScript 4 min read How to install TypeScript ? 3 min read Hello World in TypeScript 2 min read How to execute TypeScript file using command line? 2 min read Variables in TypeScript 6 min read What are the different keywords to declare variables in TypeScript ? 4 min read Identifiers and Keywords in TypeScript 2 min read TypeScript primitive typesData types in TypeScript 3 min read TypeScript Numbers 4 min read TypeScript String 4 min read Explain the concept of null and its uses in TypeScript 3 min read TypeScript Object typesWhat are TypeScript Interfaces? 4 min read TypeScript class 4 min read How enums works in TypeScript ? 4 min read TypeScript Tuples 4 min read TypeScript other typesWhat is any type, and when to use it in TypeScript ? 3 min read How to Create an Object in TypeScript? 4 min read What is an unknown type and when to use it in TypeScript ? 3 min read Explain the purpose of never type in TypeScript 3 min read TypeScript combining typesTypeScript Union 3 min read What are type aliases and how to create it in Typescript ? 3 min read TypeScript AssertionsExplain Type assertions in TypeScript 3 min read TypeScript FunctionsHow to write a function in Typescript ? 4 min read How to achieve function overloading in TypeScript ? 2 min read Explain the arrow function syntax in TypeScript 2 min read TypeScript toPrecision() Function 1 min read TypeScript toFixed() Function 2 min read TypeScript toLocaleString() Function 2 min read TypeScript toString() 1 min read TypeScript interfaces and aliasesWhat are TypeScript Interfaces? 4 min read What are type aliases and how to create it in Typescript ? 3 min read TypeScript classesHow to Extend an Interface from a class in TypeScript ? 2 min read How to Create an Object in TypeScript? 4 min read How to use getters/setters in TypeScript ? 5 min read TypeScript Inheritance 3 min read When to use interfaces and when to use classes in TypeScript ? 4 min read Generics Interface in typescript 5 min read How to use property decorators in TypeScript ? 4 min read Like