TypeScript Working with Generic Type Variables Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report In TypeScript, working with generic type variables is a powerful feature that allows you to create functions and classes that can work with a variety of data types while maintaining type safety. Note: When you declare a generic function or class, you introduce a type variable enclosed in angle brackets (<>). This type variable serves as a placeholder for a specific type that will be provided when the function or class is used. This enables you to create more flexible and reusable code. Syntax:function identity<Type>(arg: Type): Type {// Body of function}Parameters:Type is the type of datatype.Example 1: In this example, we have created an identity function that is a generic function. we will first pass a string to this and then pass a number to this. Our function is able to infer the type of variable and print it. We are also printing its type using the 'typeof()' function. JavaScript function identity<Type>(arg: Type): Type { return arg; } // The result is of type string let result = identity("GeeksforGeeks"); // The result2 is of type number let result2 = identity(42); console.log(result + " " + typeof result); console.log(result2 + " " + typeof result2); Output: Example 2: In this example, we will see what if we want to log the length of the argument arg to the console with each call. This will give us error. Make note that these type variables stand in for any and all types, so someone using this function could have passed in a number instead, which does not have a .length member. so, instead of Type we have to use Type[] as a parameter. JavaScript function identity<Type>(arg: Type): Type { return arg.length; } let result = identity(44); console.log(result) Output: Conclusion: In this article we learnt that about working with generic types in typescript. In TypeScript, working with generic type variables is a powerful feature that allows you to create functions and classes that can work with a variety of data types while maintaining type safety. Comment More infoAdvertise with us Next Article TypeScript Generic Types 21mcsrltd Follow Improve Article Tags : TypeScript Geeks Premier League 2023 Similar Reads TypeScript Working with Constrained Values TypeScript Working with Constrained Values specifies how can we deal with Constrained Values. Generic Constraints are used to specify limits to the types that can be used with generic type parameters. This results in type checking and these conditions ensure that variables have a certain type of val 2 min read TypeScript Generic Types TypeScript Generic Types can be used by programmers when they need to create reusable components because they are used to create components that work with various data types and this provides type safety. The reusable components can be classes, functions, and interfaces. TypeScript generics can be u 2 min read TypeScript Using Class Types in Generics TypeScript Using class types in generics allows you to create more flexible and reusable code by specifying that a generic parameter should be a class constructor. This is particularly useful when you want to work with instances of classes but want to keep the code type safe. You can define a generi 3 min read How to Get a Variable Type in TypeScript Understanding how to effectively ascertain the type of a variable in TypeScript is important for maintaining type safety and ensuring code robustness. In this article, we'll explore various approaches to determine the type of a variable, ranging from basic JavaScript operators to TypeScript-specific 2 min read Typescript Generic Type Array A generic type array is an array that is defined using the generic type in TypeScript. A generic type can be defined between the angled brackets(<>). Syntax:// Syntax for generic type arraylet myArray: Array<Type>;Example 1: Creating a simple generic type array of number type in Typescri 1 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 Like