TypeScript Inference with Template Literals Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report 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. Template literal types are more about working with strings and creating types based on them, not directly for linking attribute types with their callback function arguments. Example 1: In this example, we showcase the definition of AttributeTypes specific data types for attributes. The setAttribute function takes an attribute key, its new value, and a callback function. The function ensures the callback parameter aligns with the attribute's data type. The examples demonstrate how to use setAttribute to update attributes and trigger respective callbacks. JavaScript type AttributeTypes = { name: string; age: number; email: string; }; function setAttribute<K extends keyof AttributeTypes>( attribute: K, value: AttributeTypes[K], callback: (newValue: AttributeTypes[K]) => void ) { callback(value); } // Example usage: setAttribute("name", "GeeksforGeeks", (newValue) => { console.log("Setting name to:", newValue); }); setAttribute("age", 25, (newValue) => { console.log("Setting age to:", newValue); }); setAttribute("email", "[email protected]", (newValue) => { console.log("Setting email to:", newValue); }); Output: Example 2: In this example, we will see the literal type can be validated as being in the union of valid attributes in the generic. The type of the validated attribute can be looked up in the generic’s structure using Indexed Access. This typing information can then be applied to ensure the argument to the callback function is of the same type. JavaScript type AttributeNames = 'name' | 'age' | 'email'; type AttributeTypes = { name: string; age: number; email: string; }; type AttributeCallbacks = { [K in AttributeNames]: (value: AttributeTypes[K]) => void; }; function setAttribute<K extends AttributeNames>( attribute: K, value: AttributeTypes[K], callbacks: AttributeCallbacks ) { const callback = callbacks[attribute]; callback(value); } // Example usage: const callbacks: AttributeCallbacks = { name: (newValue) => { console.log("Setting name to:", newValue); }, age: (newValue) => { console.log("Setting age to:", newValue); }, email: (newValue) => { console.log("Setting email to:", newValue); }, }; setAttribute("name", "Akshit", callbacks); setAttribute("age", 25, callbacks); setAttribute("email", "[email protected]", callbacks); Output: Conclusion :This article highlights how template literal types enable aligning an attribute's data type with the first argument of its corresponding callback. Comment More infoAdvertise with us Next Article TypeScript Inference with Template Literals A abhiisaxena09 Follow Improve Article Tags : JavaScript Web Technologies Geeks Premier League TypeScript Geeks Premier League 2023 +1 More Similar Reads What are template literal types in Typescript ? 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 enhan 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 Union Type to Intersection Type in TypeScript To Transform union type to intersection type we have different approaches. In this article, we are going to learn how to Transform union type to intersection type. Below are the approaches used to Transform union type to intersection type: Table of Content Using Distributive Conditional TypesUsing C 3 min read What is the use of Infer Keyword in TypeScript ? The infer keyword in TypeScript is primarily associated with conditional types and is used to capture and assign a type to a type parameter within the scope of a conditional type. It allows TypeScript to infer the type from a given expression and use it within the definition of a more complex type. 3 min read What are Generics in TypeScript ? In this article, we will try to understand all the facts as well as the details associated with Generics in TypeScript along with some coding examples. Generics in TypeScript: Whenever any program or code is written or executed, one major thing one always takes care of which is nothing but making re 3 min read TypeScript Conditional Types In TypeScript, conditional types enable developers to create types that depend on a condition, allowing for more dynamic and flexible type definitions.They follow the syntax T extends U ? X : Y, meaning if type T is assignable to type U, the type resolves to X; otherwise, it resolves to Y.Conditiona 4 min read TypeScript Truthiness Narrowing Type In this article, we are going to learn about Truthiness narrowing Type in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. In TypeScript, truthiness narrowing is a concept that allows you to narrow down the type of a variable based on its t 3 min read TypeScript Assertions Type TypeScript Assertions Type, also known as Type Assertion, is a feature that lets developers manually override the inferred or expected type of a value, providing more control over type checking in situations where TypeScript's automatic type inference may not be sufficient.Syntaxlet variableName: As 2 min read TypeScript Hello World of Generics In this article, we will learn about Hello World of Generics in Typescript. Generics allows a user to write the code and make reusable components which further ensures the scalability and flexibility of the program or the code for a long time. âHello Worldâ of generics is the identity function that 3 min read What is a Template Literal & How is it used ? What is template literal?A template literal is simply string literals surrounded by double backticks (`). Such seemingly simple syntax opens up a treasure trove of opportunities as strings can span several lines and accept dynamic values effortlessly, thanks to expressions embedded within them. Synt 1 min read Like