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 infoAdvertise with us Next Article What are template literal types in Typescript ? L lizzywizzy366 Follow Improve Article Tags : JavaScript Web Technologies Geeks Premier League TypeScript Geeks-Premier-League-2022 JavaScript-Questions +1 More Similar Reads What are string literal types in TypeScript ? The string literal type was added in TypeScript version 1.8. String literal types work well with union types and type aliases in practice. These properties can be combined to give strings enum-like functionality. The string literal type allows you to specify a set of possible string values for a var 3 min read TypeScript Inference with Template Literals TypeScript Inference with Template Literals helps to create specific types based on string patterns. However, they're not mainly used for making sure that an attribute's type matches its callback function's argument type. TypeScript uses different methods like inference and generics for that job. Te 2 min read What are the template literals in ES6 ? Template literals are a new feature that was introduced in ECMAScript6, which offers a simple method for performing string interpolation and multiline string creation. The template literals were called template strings before the introduction of ES6. Starting from ES6 (ECMAScript 6), we have Templat 3 min read What is Type Predicates in Typescript ? In this article, we are going to learn about the type predicates in Typescript. TypeScript is a statically typed programming language that provides many features to make your code more efficient and robust. Type predicates in TypeScript are functions that return a boolean value and are used to narro 3 min read What is the Record Type in TypeScript ? In TypeScript, the Record type is a utility type that represents an object type with keys and values of a specific type. It is often used when you want to define a type for the keys and values of an object. In this article, we will learn what is the Record type in TypeScript. Syntax:Record<Keys, 2 min read What is Type Erasure in TypeScript? TypeScript is a very mighty superset of JavaScript, which adds static typing to the language and allows developers to catch errors early on as well as write more maintainable code. This being said a TypeScript type system erases type information at compile time (or during the compilation), a phenome 4 min read What are Hybrid Types in TypeScript? Hybrid types are used by TypeScript to refer to types that combine different things such as objects, functions, arrays etc. In this article, we will learn more about Hybrid Types in TypeScript.What are Hybrid Types in TypeScript?In TypeScript, using interfaces or type aliases we can define hybrid ty 2 min read Explain the Tuple Types in TypeScript TypeScript is an open-source object-oriented programming language developed and maintained by Microsoft Corporation. It is a strongly typed language and was first introduced in 2012. TypeScript is a strict superset of JavaScript, which means that anything implemented in JavaScript can be implemented 3 min read TypeScript Literal Types TypeScript's literal types allow developers to specify exact values for variables, function parameters, or properties, enhancing type safety by ensuring variables can only hold predefined values.Allow variables to have specific, exact values.Enhance code reliability by restricting permissible values 3 min read TypeScript Literal Inference Type TypeScript Literal Inference Type allows you to create type annotations based on actual values, variables, or constants. This enhances type safety and code clarity by specifying that a variable can only hold specific, exact values. It's a way to express the precise values that a variable can take on 2 min read Like